用 Java 递归地使用临时变量来反转链表。

huangapple go评论157阅读模式
英文:

Reverse a linked list recursively in java using a temp variable

问题

我在Java中实现了一个反转链表的解决方案,这是我在网上找到的。但由于某种原因,在我的代码中它不起作用。

当我打印链表时,它只打印第一个节点。我知道打印方法有效,因为当我不尝试反转时,它会打印整个链表。

在这段代码中我哪里出错了?

public class LinkedLists {
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.addLast(10);
        list.addLast(20);
        list.addLast(30);
        list.reverseList();
        list.print();
    }

    public static class LinkedList {
        private class Node {
            private int value;
            private Node next;
        }
        public Node first;
        public Node last;

        public void addLast(int item) {
            Node node = new Node();
            node.value = item;
            if (first == null) {
                first = node;
                last = node;
            } else {
                last.next = node;
                last = node;
            }
        }

        private Node reverse(Node head, Node newHead) {
            if (head == null) {
                return newHead;
            }
            Node temp = head.next;
            head.next = newHead;
            newHead = head;
            head = temp;
            return reverse(head, newHead);
        }

        public Node reverseList() {
            return reverse(first, null);
        }

        public void print() {
            Node current = first;
            while (current != null) {
                System.out.print(current.value + " ");
                current = current.next;
            }
        }
    } //class ends
}
英文:

I implemented a solution to reverse a linked list in java that I found online. But it is not working in my code for some reason.

When I print the list it only prints the first node. I know the print method works because it prints the whole thing when I don't try to reverse.

Where did I go wrong in this code?

public class LinkedLists {
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.addLast(10);
list.addLast(20);
list.addLast(30);
list.reverseList();
list.print();
}
public static class LinkedList{
private class Node{
private int value;
private Node next;
}
public Node first;
public Node last;
public void addLast(int item){
Node node = new Node();
node.value = item;
if(first == null) {
first = node;
last = node;
} else {
last.next = node;
last = node;
}
}
private Node reverse(Node head, Node newHead) {
//base case: when first = last you return
if(head == null) {
return newHead;
}
Node temp = head.next;
head.next = newHead; //this will initially be null
newHead = head;
head = temp;
return reverse(head, newHead);
}
public Node reverseList() {
return reverse(first, null);
}
public void print(){
Node current = first;
while (current != null){
System.out.print(current.value + " ");
current = current.next;
} 
}
} //class ends
}

答案1

得分: 2

虽然 reverse 返回了新头部的正确引用,但在主程序中初始调用 reverseList 的时候忽略了这个返回的引用。

你的 reverseList 方法最好不要返回任何内容,而是更新 firstlast 成员:

public void reverseList() {
    last = first;
    first = reverse(first, null);
}
英文:

Although reverse returns the correct reference for the new head, the initial call of reverseList -- in the main program -- ignores this returned reference.

Your reverseList method should better not return anything, but instead update the first and last members:

public void reverseList() {
last = first;
first = reverse(first, null);
}

huangapple
  • 本文由 发表于 2020年10月3日 08:21:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64179579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定