用于返回反向单链接列表的Java简单类

时间:2016-11-19 03:35:33

标签: java list hyperlink reverse

我正在尝试编写一个简单的类来反转单链表并返回构造的链表。如果我把一切都公之于众,那么下面的代码就可以了。有兴趣解决我的问题吗? (我应该使用双向链表?还是可以使用单链表?)

我想要的是一个函数reverseList收到ListNode Object返回ListNode Object(按相反的顺序)。像这样:

originalNumber=OriginalNumber.reverseList();

////我的代码

public class ReverseLinkList {

    public static ListNode originalNumber=new ListNode();
    public static ListNode reversedNumber=new ListNode();

    public static void main(String[] args) {
        //create 1->2->3->null
        originalNumber.add(1);originalNumber.add(2);originalNumber.add(3);
        System.out.print(num1.toString()+"\n");

        //create 3->2->1->null
        reversedNumber=originalNumber.reverseList;

    }


}

class ListNode{ 
    private class Node{
        Object data;
        Node next;
        Node(int v){
            data=v;
            next=null;
        }
        public Object getData(){
            return data;
        }
        public void setData(int v){
            data=v;
        }
        public Node getNext(){
            return next;
        }
        public void setNext(Node nextValue){
            next=nextValue;
        }
    }

    private Node head;
    public void add(int data){
        if(head==null){
            head=new Node(data);
        }
        Node temp=new Node(data);
        Node current=head;
        if(current !=null){
            while(current.getNext()!=null){
                current=current.getNext();
            }
            current.setNext(temp);
        }
    }
    public String toString(){
        String output="";
        if(head!=null){
            Node current=head.getNext();
            while(current!=null){
                //System.out.println(output);
                output+=current.getData().toString();
                current=current.getNext();
            }
        }
        return output;      
    }
    public Node getHead(){
        return head;
    }

    public static Node reverse(Node node) {
        Node prev = null;
        Node current = node;
        Node next = null;
        while (current != null) {
            next = current.next;
            current.next = prev;
            prev = current;
            current = next;
        }
        node = prev;
        return node;
    }

}

我不想要的原始和工作代码

public class ReversedLinkedList {
    static Node head;
    static class Node {
        int data;
        Node next;
        Node(int d) {
            data = d;
            next = null;
        }
    }
    Node reverse(Node node) {
        Node prev = null;
        Node current = node;
        Node next = null;
        while (current != null) {
            next = current.next;
            current.next = prev;
            prev = current;
            current = next;
        }
        node = prev;
        return node;
    }
    void printList(Node node) {
        while (node != null) {
            System.out.print(node.data + "");
            node = node.next;
        }
    }

    public static void main(String[] args) {
        ReversedLinkedList list = new ReversedLinkedList();
        list.head = new Node(1);
        list.head.next = new Node(2);
        list.head.next.next = new Node(3);

        list.printList(head);
        head = list.reverse(head);
        System.out.println("");
        list.printList(head);

    }
}

1 个答案:

答案 0 :(得分:0)

你走在正确的轨道上。您可以将成员变量设为私有&使用适当的吸气剂和设置器:

public ListNode reverseList() {
    Node prev = null;
    Node current = this.getHead();
    Node next = null;
    while (current != null) {
        next = current.next;
        current.next = prev;
        prev = current;
        current = next;
    }
    head = prev;
    return this;
}

这允许您打印反转列表:

System.out.println(originalNumber.reverseList());

请注意originalNumber本身就是被操纵的。因此,后续打印(System.out.println(originalNumber);)仍会打印反转列表。

如果您不希望修改原件,那么除了收集所有数据之外,确实没有其他任何方式。然后按相反的顺序循环播放将它们添加到新列表中:

public ListNode reverseList() {
    int size = 0;

    // Calculate size
    Node current = this.getHead();
    while (current != null) {
        size++;
        current = current.getNext();
    }
    int[] data = new int[size];

    // Collect all data
    current = this.getHead();
    int index = 0;
    while (current != null) {
        data[index++] = current.getData();
        current = current.getNext();
    }

    // Add to a new list in reverse order
    ListNode reversed = new ListNode();
    for (index = size - 1; index >= 0; index--)
        reversed.add(data[index]);
    return reversed;
}

如果在向列表添加元素时跟踪大小,或者只是切换到ArrayList而不是data的数组,则可以跳过第一次获取大小的扫描。

最后还有优雅的递归方法,它也保持了原始ListNode的完整性:

public ListNode reverseRecursive() {
    return recursive(this.getHead());
}

private ListNode recursive(Node node) {
    if (node == null)
        return new ListNode();
    else {
        ListNode listNode = this.recursive(node.next);
        listNode.add(node.data);
        return listNode;
    }
}

要打印:

System.out.println(originalNumber.reverseRecursive());

在这里,我们无需跟踪尺寸和尺寸。你利用调用堆栈自然地保持跟踪和放大反向弹出节点。