0

我正在尝试如下反转一个列表(使用类 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 循环中检查下一个元素是否为空的条件,最后一个元素被跳过。有人可以建议更改现有代码,以便可以修改 while 中的条件以对所有元素进行反转。

供参考,类 ElementL 的结构

public class ElementL{ 
        ElementL next;
        int data;


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

5 回答 5

2

你可以试试这个逻辑:-

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

return previous;

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

于 2013-09-16T10:25:02.327 回答
1

试试这个: public static ElementL reverse(ElementL element){ //这里实现reverse

        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;
    }
于 2013-09-16T10:23:45.683 回答
0

无需编写自己的自定义代码,您只需使用java.util.Collections.reverse().

于 2013-09-16T10:22:50.863 回答
0

试试这个 public static ElementL reverse(ElementL element){ //这里实现reverse

ElementL previous = null;
ElementL next = null;

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

}while(element!=null);

return  previous;

}

于 2013-09-16T11:24:05.067 回答
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();

    }
}
于 2016-02-21T14:13:54.830 回答