LeetCode: Reverse Nodes in kGroup

从这题和上一题可以总结出反转链表的经验,需要有5个指针:end, q, p, pPre, pNext. p和pPre进行方向转置后p和pPre向后移,pNext用来记录转置前p的后一个,q用来记录转置串之前的node,end记录转置串最开始的node。

 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         // Start typing your C/C++ solution below
13         // DO NOT write int main() function
14         ListNode *pPre, *end, *p, *q;
15         p = head;
16         q = NULL;
17         bool flag = true;
18         while (flag) {
19             end = pPre = p;
20             ListNode *tmp = pPre;
21             for (int i = 0; i < k; i++) {
22                 if (tmp) tmp = tmp->next;
23                 else {
24                     flag = false;
25                     break;
26                 }
27             }
28             if (!flag) break;
29             p = p->next;
30             for (int i = 0; i < k-1; i++) {
31                 ListNode *pNext = p->next;
32                 p->next = pPre;
33                 pPre = p;
34                 p = pNext;
35             }
36             end->next = p;
37             if (!q) head = pPre;
38             else q->next = pPre;
39             q = end;
40         }
41         return head;
42     }
43 };

 recursive的方法更好理解

 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) return NULL;
13         ListNode *p = head;
14         int len = 0;
15         for (; p && len < k; p = p->next) len++;
16         if (len < k) return head;
17         ListNode *pre = NULL;
18         p = head;
19         for (int i = 0; i < k; ++i) {
20             ListNode *pnext = p->next;
21             p->next = pre;
22             pre = p;
23             p = pnext;
24         }
25         head->next = reverseKGroup(p, k);
26         return pre;
27     }
28 };

 C#

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     public int val;
 5  *     public ListNode next;
 6  *     public ListNode(int x) { val = x; }
 7  * }
 8  */
 9 public class Solution {
10     public ListNode ReverseKGroup(ListNode head, int k) {
11         if (head == null) return null;
12         ListNode p = head;
13         int len = 0;
14         for (; p != null && len < k; p = p.next) len++;
15         if (len < k) return head;
16         ListNode pre = null;
17         p = head;
18         for (int i = 0; i < k; i++) {
19             ListNode pNext = p.next;
20             p.next = pre;
21             pre = p;
22             p = pNext;
23         }
24         head.next = ReverseKGroup(p, k);
25         return pre;
26     }
27 }
View Code
原文地址:https://www.cnblogs.com/yingzhongwen/p/3032232.html