Understand the reversal of singly linked lists

The requirement is very simple, input a linked list, after inverting the linked list, output the header of the new linked list.
  There are two ways to reverse a linked list (recursive method, traversal method). The interviewer’s favorite algorithm is nothing more than Fibonacci sequence and singly linked list reversal. Recursive method to achieve linked list reversal is more elegant, but it is not It is difficult for students who understand recursion.

1 | 1 recursion


Generally speaking, the recursive method starts from the last Node and replaces the pointers in order during the process of popping the stack.
Recursive method implementation diagram

In order to facilitate the understanding, we use the linked list 1->2->3->4 to demonstrate. The output effect is 4->3->2->1

First define Node:

 

public static class Node { public int value; public Node next; public Node(int data) { this.value = data; } }

The reversal method is as follows:

 

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

Recursion is essentially the process in which the system helps you push the stack, and the system keeps the scene when pushing the stack.

Let's see what a recursive process is: 1->2->3->4

  • The program enters recursion when it reaches Node newHead = reverse(head.next);
  • We assume that the recursion to 3 nodes at this time, at this time head=3 nodes, temp=3 nodes.next (actually 4 nodes)
  • Execute Node newHead = reverse(head.next); The incoming head.next is 4 nodes, and the returned newHead is 4 nodes.
  • The next step is the popping process
    • The program continues to execute temp.next = head is equivalent to 4->3
    • head.next = null means to break the pointer from the 3 node to the 4 node.
    • Return the head node newHead of the new linked list

Note: After retuen, the system will restore the scene when the 2 nodes were pushed onto the stack. At this time, head=2 nodes; temp=2 nodes.next (3 nodes), and then perform the above operations. Finally complete the flip of the entire linked list.

1 | 2 traversal method


The traversal method is to replace the pointers in order during the traversal of the linked list. First, add the code:
enter image description here

 

public static Node reverseList(Node node) { Node pre = null; Node next = null; while (node != null) { next = node.next; node.next = pre; pre = node; node = next; } return pre; }

Still 1->2->3->4

  • Prepare two empty nodes pre to save the previous node and next to make temporary variables
  • When the head node node is traversed, it is 1 node at this time
    • next = 1 node.next (2 nodes)
    • 1 node.next=pre(null)
    • pre = 1 node
    • node = 2 node
  • Perform the next cycle node=2 node
    • next = 2 nodes.next (3 nodes)
    • 2 nodes.next=pre (1 node) => complete 2->1
    • pre = 2 nodes
    • node = 3 nodes
  • Make a loop...

Guess you like

Origin blog.csdn.net/sd19871122/article/details/108110702