-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_LC25.cpp
107 lines (99 loc) · 2.12 KB
/
list_LC25.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
reverse nodes in k-Group
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
#include<iostream>
using namespace std;
#include<list>
struct ListNode {
int val;
ListNode *next;
ListNode(int x) :val(x), next(NULL) {}
};
//my solution , not complete, it is too massive to manage the whole procedure.
//so I decided to reference some codes on the Net...
/*
class Solution {
public:
ListNode * reverseKGroup(ListNode* head, int k) {
if (k == 1) {
return head;
}
//else k>=2
ListNode* temp = head;
int totalNum = 1;
while (temp ->next != NULL) {
temp = temp->next;
totalNum++;
}
int groups = totalNum / k;
int remainder = totalNum - k * groups;
ListNode* kBegin = head;
ListNode* first = head;
ListNode* second = head;
//the first group
for (int j = 0; j < k / 2; j++) {
while (k-- > j + 2) {//get the pred
first = first->next;
}
while (j-- > 0) {
second = second->next;
}
}
for (int i = 1; i < groups; i++) {
for (int j = 0; j < k/2; j++) {
while (k-- > j+2) {//get the pred
first = first->next;
}
while (j-- > 0) {
second = second->next;
}
}
}
}
};
*/
//Net solution
//new a dummy node in the beginning
class Solution {
public:
ListNode * reverseKGroup(ListNode *head, int k) {
if (!head || k == 1)return head;
ListNode *dummy = new ListNode(-1);
ListNode *pre = dummy, *cur = head;
dummy->next = head;
int i = 0;
while (cur != NULL) {
i++;
if (i%k == 0) {
pre = reverseOneGroup(pre, cur->next);
cur = pre->next;
}
else cur = cur->next;
}
return dummy->next;
}
// a reverse algorithm!!
/*
one time once node, move to the beginning.
*/
private:
ListNode * reverseOneGroup(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;
}
};