LeetCode 206 Reverse Linked List (Reverse Linked List) (4-step recursion into iteration )(*)

Source: Internet
Author: User

LeetCode 206 Reverse Linked List (Reverse Linked List) (4-step recursion into iteration )(*)
Translation

Returns a single-chain table.
Original
Reverse a singly linked list.
Analysis

I am on the grass paper 1, 2, 3, 4 For example, we will first describe the conversion process of this linked list (of course, what we draw is definitely not as refined as what we do on the blog ):

With this figure (every time I post a blog, I will find out how the figure gets so small! It can only be zoomed in or saved as an image. The figure name is 1400X600 ), Then the code will naturally come out:

ListNode* reverseList(ListNode* head) {    ListNode* newHead = NULL;    while (head) {        ListNode* nextNode = head->next;        head->next = newHead;        newHead = head;        head = nextNode;    }    return newHead;}

The above uses recursion. Next we will take a look at how to rewrite recursion into iteration. What they have in common is that all values are constantly changing. You will find that this is Head And NewHead . Therefore, we will use these two values as the parameters of the iteration loop.

Recursive Iteration

Step 1: write out the iteration template and set parameters.

ListNode* reverseListIter(ListNode* head, ListNode* newHead) {}ListNode* reverseList(ListNode* head) {}

Step 2: Set the initial value for the first iteration. If you have forgotten the code, see the recursive code above, NewHead Equal NULL So add the following code.

ListNode* reverseListIter(ListNode* head, ListNode* newHead) {}ListNode* reverseList(ListNode* head) {    return reverseListIter(head, NULL);}

Step 3: the first and second steps above can be said to be generic, but starting from step 3, we have to rewrite them according to the specific recursive process. The first is to determine the conditions for iteration stop. The conditions for stopping in the above recursion process are Head Null. Copy it here.

ListNode* reverseListIter(ListNode* head, ListNode* newHead) {    if (head == NULL) return newHead;}

Then there are two initial values in recursion, which are also copied here:

ListNode* reverseListIter(ListNode* head, ListNode* newHead) {    if (head == NULL) return newHead;    ListNode* nextNode = head->next;    head->next = newHead;    return reverseListIter(nextNode, head);}

Step 4: Update iteration parameters. The recursive code update method is as follows:

newHead = head;head = nextNode;

Of course, you can write it directly to iteration. But since parameters are used, why not simplify the process in the code form?

ListNode* reverseListIter(ListNode* head, ListNode* newHead) {    if (head == NULL) return newHead;    ListNode* nextNode = head->next;    head->next = newHead;    return reverseListIter(nextNode, head);}

This completes the entire iteration process. Great!

Note the following two points:

1. Remember to return. 2. After the first row is determined, newHead is returned. This is because when newHead is empty, newHead is returned as well as null. When newHead is not empty, newHead is returned as a result to the reverseList function.

In fact, it is very easy to understand the process of rewriting. I hope my blog can help you ......

Code
/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/class Solution {public:    ListNode* reverseListIter(ListNode* head, ListNode* newHead) {        if (head == NULL) return newHead;        ListNode* nextNode = head->next;        head->next = newHead;        return reverseListIter(nextNode, head);    }    ListNode* reverseList(ListNode* head) {        return reverseListIter(head, NULL);    }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.