24. Swap Nodes in Pairs(M);25. Reverse Nodes in k-Group(H)
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
- Total Accepted: 156137
- Total Submissions: 413794
- Difficulty: Medium
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode *swapPairs(ListNode *head) { 12 if (head == nullptr || head->next == nullptr) return head; 13 ListNode dummy(-1); 14 dummy.next = head; 15 for(ListNode *prev = &dummy, *cur = prev->next, *next = cur->next; 16 next; 17 prev = cur, cur = cur->next, next = cur ? cur->next: nullptr) { 18 prev->next = next; 19 cur->next = next->next; 20 next->next = cur; 21 } 22 return dummy.next; 23 } 24 };
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* swapPairs(ListNode* head) 12 { 13 ListNode dummy(-1); 14 dummy.next = head; 15 ListNode *p = &dummy, *q = head; 16 while (q && q->next) 17 { 18 p->next = q->next; 19 q->next = q->next->next; 20 p->next->next = q; 21 p = q; 22 q = q->next; 23 } 24 return dummy.next; 25 } 26 };
3m 37.45%
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. 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
- Total Accepted: 89044
- Total Submissions: 294580
- Difficulty: Hard
解题思路:节点指针的指向变换比较麻烦.
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { //by guxuanqing@gmail.com 10 public: 11 ListNode* reverseKGroup(ListNode* head, int k) { 12 int len = 0; 13 ListNode dummy(-1); 14 dummy.next = head; 15 if(k == 1) return dummy.next; 16 ListNode *p = &dummy, *prevq = &dummy, *q = head; 17 while (p->next) { 18 p = p->next; 19 ++len; 20 } 21 p = &dummy; 22 div_t dv = div(len, k); 23 int shang = dv.quot; 24 25 while (shang--) 26 { 27 ListNode *tmpp = q; 28 prevq = q; 29 q = q->next; 30 int tmpk = k - 1; 31 while (tmpk--) 32 { 33 ListNode *qq = q->next; 34 q->next = prevq; 35 prevq = q; 36 q = qq; 37 } 38 tmpp->next = q; 39 p->next = prevq; 40 p = tmpp; 41 prevq = tmpp; 42 } 43 return dummy.next; 44 } 45 };
26ms 31.72%
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode *reverseKGroup(ListNode *head, int k) { 12 if (head == nullptr || head->next == nullptr || k < 2) return head; 13 ListNode dummy(-1); 14 dummy.next = head; 15 for(ListNode *prev = &dummy, *end = head; end; end = prev->next) { 16 for (int i = 1; i < k && end; i++) 17 end = end->next; 18 if (end == nullptr) break; 19 prev = reverse(prev, prev->next, end); 20 } 21 return dummy.next; 22 } 23 ListNode* reverse(ListNode *prev, ListNode *begin, ListNode *end) { 24 ListNode *end_next = end->next; 25 for (ListNode *p = begin, *cur = p->next, *next = cur->next; 26 cur != end_next; 27 p = cur, cur = next, next = next ? next->next : nullptr) { 28 cur->next = p; 29 } 30 begin->next = end_next; 31 prev->next = end; 32 return begin; 33 } 34 };
26ms