使用do While循环反转链接列表

时间:2013-09-16 10:18:18

标签: algorithm data-structures

我正在尝试反转List(由使用Class ElementL创建),如下所示,我可以使用以下方法将其反转,但在反向时跳过最后一个元素。

public static ElementL reverse(ElementL element){
        //Implement reverse here

        ElementL previous = null;
        ElementL next = element.next;

        do{
            element.next = previous;
            previous = element;
            element = next;
            next = next.next;
        }while(next!=null);

        return  previous;
    }

由于While循环中的条件检查下一个元素是否为null,因此跳过了最后一个元素。有人可以建议更改现有代码,以便可以修改while中的条件以对所有元素执行反向操作。

参考,类ElementL的结构

public class ElementL{ 
        ElementL next;
        int data;


        public ElementL(int data){
            this.data = data;
            this.next = null;
        }       
    }   

5 个答案:

答案 0 :(得分:2)

你可以试试这个逻辑: -

ElementL previous = null;
ElementL next = null;
do {
    next = element.next;
    element.next = previous;
    previous = element;
    element = next;
} while (next != null);

return previous;

您唯一需要关注的是,element最初不为空!

答案 1 :(得分:1)

试试这个:     public static ElementL reverse(ElementL element){             //在这里实施反向

        ElementL previous = null;
        ElementL next = element.next;

        do{
            element.next = previous;
            previous = element;
            element = next;
            if (next != null)
               next = next.next;
        }while(element!=null);

        return  previous;
    }

答案 2 :(得分:0)

您可以使用java.util.Collections.reverse()

,而不是编写自己的自定义代码

答案 3 :(得分:0)

试试这个 public static ElementL reverse(ElementL element){     //在这里实施反向

ElementL previous = null;
ElementL next = null;

do{
    next = element.next;
    element.next = previous;
    previous = element;
    element = next;

}while(element!=null);

return  previous;

}

答案 4 :(得分:0)

class Node {
    private Node next;
    private int value;

    public Node() {
    }

    public Node(Node next, int value) {
        super();
        this.next = next;
        this.value = value;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

}

public class Linkedlist {
    private Node head = null;

    public Linkedlist(Node head) {
        this.head = head;
    }

    public void iterate() {
        Node current = head;
        while (current != null) {
            System.out.println(current.getValue());
            current = current.getNext();
        }
    }

    public void reverse() {
        Node current = head;
        Node prev = null;
        while (current != null) {
            Node temp = current.getNext();
            current.setNext(prev);
            prev = current;
            current = temp;
        }
        head = prev;
    }

    public static void main(String[] args) {
        Node n = new Node(null, 10);
        Node n1 = new Node(n, 20);
        Node n2 = new Node(n1, 30);
        Node n3 = new Node(n2, 40);
        Node n4 = new Node(n3, 50);
        Node n5 = new Node(n4, 60);
        Linkedlist linkedlist = new Linkedlist(n5);
        linkedlist.iterate();
        linkedlist.reverse();
        System.out.println("------------------REVERSED---------------------");
        linkedlist.iterate();

    }
}