C++實現LeetCode(25.每k個一組翻轉鏈表)
[LeetCode] 25. Reverse Nodes in k-Group 每k個一組翻轉鏈表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
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
Note:
- Only constant extra memory is allowed.
- You may not alter the values in the list’s nodes, only nodes itself may be changed.
這道題讓我們以每k個為一組來翻轉鏈表,實際上是把原鏈表分成若幹小段,然後分別對其進行翻轉,那麼肯定總共需要兩個函數,一個是用來分段的,一個是用來翻轉的,以題目中給的例子來看,對於給定鏈表 1->2->3->4->5,一般在處理鏈表問題時,大多時候都會在開頭再加一個 dummy node,因為翻轉鏈表時頭結點可能會變化,為瞭記錄當前最新的頭結點的位置而引入的 dummy node,加入 dummy node 後的鏈表變為 -1->1->2->3->4->5,如果k為3的話,目標是將 1,2,3 翻轉一下,那麼需要一些指針,pre 和 next 分別指向要翻轉的鏈表的前後的位置,然後翻轉後 pre 的位置更新到如下新的位置:
–1->1->2->3->4->5
| | |
pre cur next–1->3->2->1->4->5
| | |
cur pre next
以此類推,隻要 cur 走過k個節點,那麼 next 就是 cur->next,就可以調用翻轉函數來進行局部翻轉瞭,註意翻轉之後新的 cur 和 pre 的位置都不同瞭,那麼翻轉之後,cur 應該更新為 pre->next,而如果不需要翻轉的話,cur 更新為 cur->next,代碼如下所示:
解法一:
class Solution { public: ListNode* reverseKGroup(ListNode* head, int k) { if (!head || k == 1) return head; ListNode *dummy = new ListNode(-1), *pre = dummy, *cur = head; dummy->next = head; for (int i = 1; cur; ++i) { if (i % k == 0) { pre = reverseOneGroup(pre, cur->next); cur = pre->next; } else { cur = cur->next; } } return dummy->next; } ListNode* reverseOneGroup(ListNode* pre, ListNode* next) { ListNode *last = pre->next, *cur = last->next; while(cur != next) { last->next = cur->next; cur->next = pre->next; pre->next = cur; cur = last->next; } return last; } };
我們也可以在一個函數中完成,首先遍歷整個鏈表,統計出鏈表的長度,然後如果長度大於等於k,交換節點,當 k=2 時,每段隻需要交換一次,當 k=3 時,每段需要交換2此,所以i從1開始循環,註意交換一段後更新 pre 指針,然後 num 自減k,直到 num<k 時循環結束,參見代碼如下:
解法二:
class Solution { public: ListNode* reverseKGroup(ListNode* head, int k) { ListNode *dummy = new ListNode(-1), *pre = dummy, *cur = pre; dummy->next = head; int num = 0; while (cur = cur->next) ++num; while (num >= k) { cur = pre->next; for (int i = 1; i < k; ++i) { ListNode *t = cur->next; cur->next = t->next; t->next = pre->next; pre->next = t; } pre = cur; num -= k; } return dummy->next; } };
我們也可以使用遞歸來做,用 head 記錄每段的開始位置,cur 記錄結束位置的下一個節點,然後調用 reverse 函數來將這段翻轉,然後得到一個 new_head,原來的 head 就變成瞭末尾,這時候後面接上遞歸調用下一段得到的新節點,返回 new_head 即可,參見代碼如下:
解法三:
class Solution { public: ListNode* reverseKGroup(ListNode* head, int k) { ListNode *cur = head; for (int i = 0; i < k; ++i) { if (!cur) return head; cur = cur->next; } ListNode *new_head = reverse(head, cur); head->next = reverseKGroup(cur, k); return new_head; } ListNode* reverse(ListNode* head, ListNode* tail) { ListNode *pre = tail; while (head != tail) { ListNode *t = head->next; head->next = pre; pre = head; head = t; } return pre; } };
到此這篇關於C++實現LeetCode(25.每k個一組翻轉鏈表)的文章就介紹到這瞭,更多相關C++實現每k個一組翻轉鏈表內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- C++實現LeetCode(86.劃分鏈表)
- C++實現LeetCode(24.成對交換節點)
- C++實現LeetCode(203.移除鏈表元素)
- C++實現LeetCode(21.混合插入有序鏈表)
- C++實現LeetCode(148.鏈表排序)