Leetcode之Reverse Linked List I II

206. Reverse Linked List

Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
Follow up:
A linked list can be reversed either iteratively or recursively. Could you implement both?

The title lets us implement iteratively and recursively, respectively. Let's first look at the iterative solution. The idea is to get the next node, change the current node's point, and move the pointer down until the end of the linked list.

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* pre = NULL;
        ListNode* cur = head;
        while (cur) {
            ListNode* temp = cur->next;
            cur->next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }
};

For the recursive implementation, keep entering the recursive function until head points to the last node, p points to the previous node, then swap the positions of head and p, and then return to the previous recursive function, and then swap the positions of p and head, each exchange After that, the head node is followed by the exchanged order, until p is the first node, and then exchanged, the first node becomes the node, and the entire linked list is also flipped.

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

92. Reverse Linked List II

Reverse a linked list from position m to n. Do it in one-pass.
Note: 1 ≤ m ≤ n ≤ length of list.
Example:
Input: 1->2->3->4->5->NULL, m = 2, n = 4
Output: 1->4->3->2->5->NULL

The place where this road acts as an extension is to invert a small section of it. For the problem of linked list, according to past experience, it is generally necessary to build a dummy node and connect it to the head node of the original linked list. In this case, even if the head node changes, we can also obtain the head node of the new linked list through dummy->next. point. The requirement of this question is to complete only one traversal. Taking the example in the question as an example, the three points 2, 3, and 4 are transformed. Then we can first take out 2, use the front pointer to point to 2, and then when taking out At 3, we add 3 to the front of 2, move the front pointer to 3, and so on, and stop after 4, so we get a new linked list 4->3->2, and the front pointer points to 4. For the original linked list, the positions of two points are very important and need to be recorded with pointers, namely 1 and 5, because when 2, 3, and 4 are taken away, the original linked list becomes 1->5- >NULL, the positions of these two points are required when inserting a new linked list. The position of 1 is easy to find, because we know the value of m, we use the pre pointer to record the position of 1, and the position of 5 can be recorded at the end. When the 4 node is taken away, the position of 5 needs to be recorded, so that we can put The small segment of the linked list after inversion is added to the original linked list.

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseBetween(ListNode* head, int m, int n) {
        ListNode *dummy = new ListNode(-1);
        dummy->next = head;
        ListNode *cur = dummy;
        ListNode *pre, *front, *last;
        for (int i = 1; i <= m - 1; ++i) cur = cur->next;
        pre = cur;
        last = cur->next;
        for (int i = m; i <= n; ++i) {
            cur = pre->next;
            pre->next = cur->next;
            cur->next = front;
            front = cur;
        }
        cur = pre->next;
        pre->next = front;
        last->next = cur;
        return dummy->next;
    }
};

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325599992&siteId=291194637