-
Notifications
You must be signed in to change notification settings - Fork 1
/
25.k-个一组翻转链表.cpp
46 lines (43 loc) · 1.05 KB
/
25.k-个一组翻转链表.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
* @lc app=leetcode.cn id=25 lang=cpp
*
* [25] K 个一组翻转链表
*
* 1. 组内指向翻转
* 2. 组头、组尾指对
*/
// @lc code=start
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
auto dummy = new ListNode();
dummy->next = head;
auto p = dummy;
while(true) {
auto q = p;
for (int i = 0; i < k && q; i++) q = q ->next;
if (!q) break;
auto a = p->next, b = a->next;
for (int i = 0; i < k - 1; i++) {
auto c = b->next;
b->next = a;
a = b, b = c;
}
auto c = p->next;
p->next = a, c->next = b;
p = c;
}
return dummy->next;
}
};
// @lc code=end