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

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

Reverse a linked list recursively in java using a temp variable

问题

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

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

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

  1. public class LinkedLists {
  2. public static void main(String[] args) {
  3. LinkedList list = new LinkedList();
  4. list.addLast(10);
  5. list.addLast(20);
  6. list.addLast(30);
  7. list.reverseList();
  8. list.print();
  9. }
  10. public static class LinkedList {
  11. private class Node {
  12. private int value;
  13. private Node next;
  14. }
  15. public Node first;
  16. public Node last;
  17. public void addLast(int item) {
  18. Node node = new Node();
  19. node.value = item;
  20. if (first == null) {
  21. first = node;
  22. last = node;
  23. } else {
  24. last.next = node;
  25. last = node;
  26. }
  27. }
  28. private Node reverse(Node head, Node newHead) {
  29. if (head == null) {
  30. return newHead;
  31. }
  32. Node temp = head.next;
  33. head.next = newHead;
  34. newHead = head;
  35. head = temp;
  36. return reverse(head, newHead);
  37. }
  38. public Node reverseList() {
  39. return reverse(first, null);
  40. }
  41. public void print() {
  42. Node current = first;
  43. while (current != null) {
  44. System.out.print(current.value + " ");
  45. current = current.next;
  46. }
  47. }
  48. } //class ends
  49. }
英文:

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?

  1. public class LinkedLists {
  2. public static void main(String[] args) {
  3. LinkedList list = new LinkedList();
  4. list.addLast(10);
  5. list.addLast(20);
  6. list.addLast(30);
  7. list.reverseList();
  8. list.print();
  9. }
  10. public static class LinkedList{
  11. private class Node{
  12. private int value;
  13. private Node next;
  14. }
  15. public Node first;
  16. public Node last;
  17. public void addLast(int item){
  18. Node node = new Node();
  19. node.value = item;
  20. if(first == null) {
  21. first = node;
  22. last = node;
  23. } else {
  24. last.next = node;
  25. last = node;
  26. }
  27. }
  28. private Node reverse(Node head, Node newHead) {
  29. //base case: when first = last you return
  30. if(head == null) {
  31. return newHead;
  32. }
  33. Node temp = head.next;
  34. head.next = newHead; //this will initially be null
  35. newHead = head;
  36. head = temp;
  37. return reverse(head, newHead);
  38. }
  39. public Node reverseList() {
  40. return reverse(first, null);
  41. }
  42. public void print(){
  43. Node current = first;
  44. while (current != null){
  45. System.out.print(current.value + " ");
  46. current = current.next;
  47. }
  48. }
  49. } //class ends
  50. }

答案1

得分: 2

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

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

  1. public void reverseList() {
  2. last = first;
  3. first = reverse(first, null);
  4. }
英文:

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:

  1. public void reverseList() {
  2. last = first;
  3. first = reverse(first, null);
  4. }

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:

确定