Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在列表的末尾添加了一些注释,但如果没有某种迭代,我看不出这将如何发生。所以在我看来,要么递归调用不断迭代直到它达到null,要么head.next不断迭代直到它找到null!这就是我被困的地方。谢谢“但我理解你的评论”:不确定你指的是上面哪一条评论。或者这_Java_C#_Recursion_Linked List_Singly Linked List - Fatal编程技术网

Java 在列表的末尾添加了一些注释,但如果没有某种迭代,我看不出这将如何发生。所以在我看来,要么递归调用不断迭代直到它达到null,要么head.next不断迭代直到它找到null!这就是我被困的地方。谢谢“但我理解你的评论”:不确定你指的是上面哪一条评论。或者这

Java 在列表的末尾添加了一些注释,但如果没有某种迭代,我看不出这将如何发生。所以在我看来,要么递归调用不断迭代直到它达到null,要么head.next不断迭代直到它找到null!这就是我被困的地方。谢谢“但我理解你的评论”:不确定你指的是上面哪一条评论。或者这,java,c#,recursion,linked-list,singly-linked-list,Java,C#,Recursion,Linked List,Singly Linked List,在列表的末尾添加了一些注释,但如果没有某种迭代,我看不出这将如何发生。所以在我看来,要么递归调用不断迭代直到它达到null,要么head.next不断迭代直到它找到null!这就是我被困的地方。谢谢“但我理解你的评论”:不确定你指的是上面哪一条评论。或者这是对我答案的评论?在这种情况下,你能在下面回答我的评论吗?我在中间和结尾加了一些解释。请让我知道这是否澄清了它的工作原理。 static Node reverse(Node head) { if(head == null) {


在列表的末尾添加了一些注释,但如果没有某种迭代,我看不出这将如何发生。所以在我看来,要么递归调用不断迭代直到它达到null,要么head.next不断迭代直到它找到null!这就是我被困的地方。谢谢“但我理解你的评论”:不确定你指的是上面哪一条评论。或者这是对我答案的评论?在这种情况下,你能在下面回答我的评论吗?我在中间和结尾加了一些解释。请让我知道这是否澄清了它的工作原理。
static Node reverse(Node head) {
    if(head == null) {
        return head;
    }

    // last node or only one node
    if(head.next == null) {
        return head;
    }

    Node newHeadNode = reverse(head.next);

    // change references for middle chain
    head.next.next = head;
    head.next = null;

    // send back new head node in every recursion
    return newHeadNode;
}

public static void main(String[] args) throws IOException {
    LinkedList llist = new LinkedList();
    llist.insertNode(20);
    llist.insertNode(4);
    llist.insertNode(15);
    llist.insertNode(85);
      
    Node llistReversed = reverse(llist.head);
    printSinglyLinkedList(llistReversed, " ");
}
A>B>C>D
A>B>C><D
  null
    ᐱ
A>B>C<D
A>B><C<D
null 
 ᐱ
 A<B<C<D
if (head.next == null) {
    return head;
}
 head
  ↓              
┌───────────┐    ┌───────────┐                        ┌───────────┐
│ value: 85 │    │ value: 15 │                        │ value: 20 │
│ next: ———————→ │ next: ———————→ ...more nodes...——→ │ next:null │
└───────────┘    └───────────┘                        └───────────┘ 
Node newHeadNode = reverse(head.next);
                  head
                   ↓              
                 ┌───────────┐                        ┌───────────┐
                 │ value: 15 │                        │ value: 20 │
                 │ next: ———————→ ...more nodes...——→ │ next:null │
                 └───────────┘                        └───────────┘ 
                  head                                (returned)
                   ↓                                    ↓        
                 ┌───────────┐                        ┌───────────┐
                 │ value: 15 │                        │ value: 20 │
                 │ next:null │ ←——...more nodes... ←——————— :next │
                 └───────────┘                        └───────────┘ 
 head                                                  newHeadNode
  ↓                                                     ↓
┌───────────┐    ┌───────────┐                        ┌───────────┐
│ value: 85 │    │ value: 15 │                        │ value: 20 │
│ next: ———————→ │ next:null │ ←——...more nodes... ←——————— :next │
└───────────┘    └───────────┘                        └───────────┘ 
head.next.next = head;
 head                                                  newHeadNode
  ↓                                                     ↓
┌───────────┐    ┌───────────┐                        ┌───────────┐
│ value: 85 │    │ value: 15 │                        │ value: 20 │
│ next: ———————→ │           │                        │           │
│           │ ←——————— :next │ ←——...more nodes... ←——————— :next │
└───────────┘    └───────────┘                        └───────────┘ 
head.next = null;
 head                                                  newHeadNode
  ↓                                                     ↓
┌───────────┐    ┌───────────┐                        ┌───────────┐
│ value: 85 │    │ value: 15 │                        │ value: 20 │
│ next: null│ ←——————— :next │ ←——...more nodes... ←——————— :next │
└───────────┘    └───────────┘                        └───────────┘ 
return newHeadNode;