The reverse of a single-chain table is always remembered.
The reverse placement of a single-chain table can be implemented in multiple ways. This article is a summary of the reverse placement problem:
First, use the three-pointer method. The three pointers are used to respectively record the precursor nodes of each node, its own nodes, and the post-drive nodes. The while loop is used to continuously adjust the order, and then move the operation point by point. The three pointers in China should be the simplest way and should be given priority.
LNode* ReverseList(LNode* head){if (head == NULL)return NULL;if (head->next == NULL)return head;LNode* pre = NULL;LNode* Cur = head;LNode* Nex = NULL;while (Cur!=NULL){Nex = Cur->next;Cur->next = pre;pre = Cur;Cur = Nex;}return pre;}
Because the reverse operation on a single-chain table in the past prompted me to consider recursive solutions, but I am confused about the recursive method. If you do not understand it, I still know a little about it, make sure you understand it. Today, I finally wrote a solution.
Lnode * reverlist (lnode * head, lnode * & headl) {If (Head = NULL) return NULL; If (Head-> next = NULL) {headl = head; // return and nod return head;} If (Head-> next! = NULL) {lnode * node = head; // retain the previous node lnode * tempnode = reverlist (Head-> next, headl); tempnode-> next = node; // reset node-> next = NULL; // This sentence must exist; otherwise, the return node of the cyclic linked list will be generated; // return the End Node of the reverse linked list }}
The above implementation is based on the following points:
1. perform a security check on the linked list operation to determine whether the header node is empty.
2. There must be an end point for Recursive Implementation. The end point of this article is to find the last node. It can also be understood that a linked list can be returned directly when there is only one end point.
3. with regard to recursion, the last node is returned, which must be reversed, which changes the point of the node pointer. Therefore, the previous node (lnode * node = head) must be saved ), the current node refers to the forward node.
4. the next of the previous node must be null. Otherwise, a circular linked list is generated when the node exits. If 3-> 2-> 1-> 2-> ....... -> 1-> ........ (To solve this problem, you can recursively set two parameters (PRE, cur), pre = NULL when calling)
5. At the end of recursion at each layer, the End Node of the inverse linked list must be returned to prepare for Recursive inversion at the previous layer.
6. the first node of the inverse linked list is the end node of the original linked list, but it is still confusing for Recursive exit. Therefore, head1 is added to store the header node of the inverse linked list, and the reference type is used.
Conclusion: This is not ideal.
Harvest: recursion is actually a process of going into the stack and going out of the stack. the next layer of recursion is a new process of going into the stack. In the new stack, we can save known information, just like this question, you can not only save the head, but also save the head-> next. after the underlying function outputs the stack and returns it to the stack, the stored information is still usable. At first, you just want to save a node head. Head-> next needs to be specified through the return value, obviously, this is also caused by insufficient understanding of recursion.
Each node of the linked list must define its next to prevent chain breaks or loops. Test Cases can include null and only one node.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~
Because the reference method is not good, and no recursion will be returned using the reference method, I have reflected on my program by referring to the blog without trace, it is found that the current and next nodes can be stored before recursion. Make recursive return values always the first node of the inverse linked list.
Lnode * reverlist1 (lnode * head) {If (Head = NULL) return NULL; If (Head-> next = NULL) {return head;} lnode * node = head; // retain the previous node lnode * NEX = head-> next; lnode * headnode = reverlist1 (NEX); NEX-> next = node; // reset node-> next = NULL; // This sentence must exist; otherwise, the return headnode of the cyclic linked list will be generated; // return the End Node of the reverse linked list}
Features of this recursion:
1. Save the current and next nodes when entering recursion. Use the next node for Recursive end judgment.
2. Recursive return values always carry the End Node of the original linked list and the first node of the reverse linked list. Recursive return processes are not affected.
3. During the reverse operation, adjacent nodes are stored before recursion. It does not affect the returned first node. It can be considered as a stack import process: the current and next nodes are stored each time the stack is pushed in. recursion means continuous stack import, and recursive return means continuous stack export. The reverse setting is to use the stored adjacent nodes to complete the reverse operation.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Directly use
LNode* Re(LNode* head){if (head ==NULL)return NULL;if (head->next == NULL)return head;LNode* Node = Re(head->next);head->next->next = head;head->next = NULL;return Node;}