Reverse Nodes in k-Group leetcode

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.

If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

You may not alter the values in the nodes, only nodes itself may be changed.

Only constant memory is allowed.

For example,
Given this linked list: 1->2->3->4->5

For k = 2, you should return: 2->1->4->3->5

For k = 3, you should return: 3->2->1->4->5

首先,搞清楚怎麼逆轉一個單鏈表。其實O(n)就可以了。第一個肯定是last one。然後我們每遍歷到一個node,就把它放到最鏈表的首位,

最後一個麼,最後就成爲第一個了。下面是一個簡單逆轉鏈表的程序。

ListNode* helper = new ListNode(0);
 helper->next = head;
ListNode* pre = helper;
ListNode* last = head;
ListNode* cur = head->next;
 while(cur != nullptr) {
            last->next = cur->next;
            cur->next = pre->next;
            pre->next = cur;
            cur = last->next;
        }
        return helper->next;
因爲有“放到鏈表首位”的操作,我們需要一個dummy的頭節點,遇到的新節點我們simply state: pre.next = cur; 保持一個invariant就是last節點始終在最後(cur的前面一個)

然後我們有如下方法:

    ListNode* reverseList(ListNode* pre,ListNode* next) {
        ListNode* last = pre->next;
        ListNode* cur = last->next;
        while(cur != next) {
            last->next = cur->next;
            cur->next = pre->next;
            pre->next = cur;
            cur = last->next;
        }
        return last;
    }

就是區間的reverse。因爲題目要求的是k group逆轉嘛。注意人返回的是最後一個(last)節點,這樣下一個k-group就可以用上了

   ListNode *reverseKGroup(ListNode *head, int k) {
        if(k<=1 || head == nullptr)
            return head;
        ListNode* helper = new ListNode(0);
        helper->next = head;
        ListNode* pre = helper;
        int i = 0;
        while(head != nullptr) {
            i++;
            if(i % k == 0) {
                pre = reverseList(pre,head->next);
                head = pre->next;
            } else {
                head = head->next;
            }
        }
        return helper->next;
    }

還有一個添加頭結點的例子:鏈表的插入排序:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *insertionSortList(ListNode *head) {
        if(head == nullptr || head->next == nullptr)
            return head;
        ListNode* helper = new ListNode(0);
        helper->next = head;
        ListNode* pre = helper;
        ListNode* sortCur = helper->next;
        ListNode* sortEnd = head;
        ListNode* cur = head->next;
        while(cur != nullptr) {
            while(sortCur != cur && sortCur->val <= cur->val) {
                pre = pre->next;
                sortCur = sortCur->next;
            }
            if(sortCur == cur) {
                sortEnd = cur;
            } else {
                sortEnd->next = cur->next;
                cur->next = sortCur;
                pre->next = cur;
            }
            cur = sortEnd->next;
            pre = helper;
            sortCur = helper->next;
        }
        head = helper->next;
        delete helper;
        return head;
    }
};


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章