2, single linked list reverse order
The second topic is the classic "single linked list reverse" problem. Many companies have this problem in the interview questions, some companies clear the problem requirements can not use additional node storage space, some did not specify, but if the interviewer used the additional node storage space to do the relay, will get a relatively low score. How to reverse all nodes of a single linked list without using an additional storage node. We first use the idea of iterative loops to analyze this problem, the initial state of the list is shown in figure (1):
Figure (1) Initial state
Initial state, Prev is Null,head point to the current header node A,next to the next node B of Node A. Start with a node in reverse order, point a node next pointer to Prev, because the current value of prev is null, so a node out of the list, and then move the head and next pointers, So that they point to the next node C of B and B (because the current next already points to the B node, so modifying the next pointer of a node does not cause the linked list to be lost). After the reverse node A, the status of the list is shown in Figure (2):
Figure (2) after the first iteration of the state
A total of four operations were done from the initial state of diagram (1) to the state of diagram (2), and the pseudo code for these four operations is as follows:
Head->next = prev;
prev = head;
Head = Next;
Next = head->next;
These four lines of pseudocode are the iterations of the loop algorithm, and now use the iterator to iterate over the state of the diagram (2) and get the state of the diagram (3):
Figure (3) after the second iteration of the state
So what about the cycle termination condition. Now iterate over the state of the diagram (3) again to get the state of the diagram (4):
Figure (4) after the third iteration of the state
At this point, we can see that in the diagram (4) on the basis of another iteration can complete the reverse order of the list, so the loop iteration termination condition is the current head pointer is null.
Now to sum up, the initial conditions of the cycle are:
prev = NULL;
The loop iteration body is:
Next = head->next;
Head->next = prev;
prev = head;
Head = Next;
The loop termination condition is:
Head = = NULL
Based on the above analysis, the cyclic algorithm of the single linked list in reverse order is shown as follows:
Link_node *reverselink (Link_node *head)
62 {
*next Link_node;
Link_node *prev = NULL;
65
"While" (Head!= NULL)
67 {
Next = head->next;
Head->next = prev;
prev = head;
Head = Next;
72}
73
Prev return;
75}
Now, we use recursive thinking to analyze this problem. Let's assume that there is a function that can reverse the single linked list of head nodes and return a new head node pointer, which should look like this:
Link_node *reverselink2 (Link_node *head)
Now using REVERSELINK2 () to solve the problem, the list is divided into the current table head node and the rest of the nodes, the idea of recursion is to first remove the current table head node from the list, and then reverse the remaining nodes, and finally connect the current header node to the end of the new list. The state of the first recursive call to the REVERSELINK2 (Head->next) function is shown in figure (5):
Figure (5) First recursive state diagram
The key point here is the next node in the head node header head->next will be the tail node of the new linked list after the reverse sequence, that is, the removed head contact heads need to be connected to the Head->next to complete the order of the entire list. The core of a recursive algorithm is a few lines of code:
Newhead = ReverseLink2 (Head->next); /* Recursive part * *
Head->next->next = head; * * Back part of the moon
Head->next = NULL;
Now follow this line of thought again, and get a second recursive state diagram:
Figure (6) Second recursive state diagram
Once again recursive analysis, you can clearly see the recursive termination conditions:
Figure (7) Third recursive state diagram
A recursive termination condition is a pointer that returns directly to a node when a list is left. We can see that the core of this algorithm is actually in the back part, the purpose of recursion is to traverse to the tail node of the list, and then through the gradual return of the node next pointer flip over. The complete code for the recursive algorithm is as follows:
Link_node *reverselink2 (Link_node *head)
78 {
Link_node *newhead;
80
Bayi if (head = = NULL) | | (Head->next = NULL))
The return head;
83
Newhead = ReverseLink2 (Head->next); /* Recursive part * *
Head->next->next = head; * * Back part of the moon
Head->next = NULL;
87
The return newhead;
89}
Loops or recursion. This is a problem. When faced with a problem, you can not think of which algorithm is good, which is bad, but according to the type and size of the problem to make a choice. For linear data structure, it is more suitable to use iterative loop method, but for tree-like data structure, such as two-fork tree, recursive method is very simple and elegant.