-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path25.py
235 lines (206 loc) · 6.7 KB
/
25.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
__________________________________________________________________________________________________
48ms
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def reverseGroup(self, head: 'ListNode') -> 'ListNode':#翻转链表
if head.next == None:
return head
dummy = ListNode(0)
dummy.next = head
tmp = dummy
last = None
while tmp and tmp.next:
tmp1 = tmp.next.next
tmp.next.next = tmp
tmp2 = tmp.next
if last:
tmp.next = last
last = tmp2
tmp = tmp1
else:
dummy.next.next = None
if tmp == None:
dummy = last
elif tmp.next == None:
tmp.next = last
dummy = tmp
return dummy
def reverseKGroup(self, head: 'ListNode', k: 'int') -> 'ListNode':
dummy = ListNode(0)
dummy.next = head
start = head
last = dummy
end = dummy
for i in range(k):
end = end.next
if end == None:
break
#dummy->0->1->2->3->4->5
while end:
tmp1 = start
start = end.next
tmp2 = end
end = tmp1
tmp2.next = None
last.next = self.reverseGroup(tmp1)
end.next = start
last = end
for i in range(k):
end = end.next
if end == None:
break
return dummy.next
__________________________________________________________________________________________________
52ms
class Solution:
def reverseKGroup(self, head, k):
p = head
p1 = head
i = 0
while p and i < k:
p = p.next
i += 1
if i < k:
return head
newhead = self.reverseKGroup(p,k)
for i in range(k):
p2 = p1.next
p1.next = newhead
newhead = p1
p1 = p2
return newhead
__________________________________________________________________________________________________
56ms
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
new_head = None
new_tail = head
old_tail = None
prv_node = None
cur_node = head
nxt_node = None
count = 0
while cur_node is not None:
if count is 0:
tmp_node = cur_node
tmp_count = 0
while tmp_node is not None and tmp_count < k:
tmp_node = tmp_node.next
tmp_count += 1
if tmp_count < k:
if old_tail is None:
return head
else:
old_tail.next = cur_node
break
else:
new_tail = cur_node
if count < k - 1:
nxt_node = cur_node.next
cur_node.next = prv_node
prv_node = cur_node
cur_node = nxt_node
count += 1
elif count == k - 1:
if new_head is None:
new_head = cur_node
nxt_node = cur_node.next
cur_node.next = prv_node
prv_node = cur_node
cur_node = nxt_node
else:
nxt_node = cur_node.next
cur_node.next = prv_node
prv_node = cur_node
old_tail.next = cur_node
cur_node = nxt_node
new_tail.next = None
count += 1
else:
count = 0
prv_node = new_tail
old_tail = new_tail
return new_head
__________________________________________________________________________________________________
13068 kb
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def reverseKGroup(self, head: 'ListNode', k: 'int') -> 'ListNode':
# 1. edge case k = 1, len(sub) < k
if k < 2: return head
node = head
for _ in range(k):
if not node:
return head
node = node.next
# reverse
curr = head
prev = None
for _ in range(k):
temp = curr.next
curr.next = prev
prev = curr
curr = temp
#recursive
head.next = self.reverseKGroup(curr, k)
return prev
__________________________________________________________________________________________________
13080 kb
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def reverseKGroup(self, head: 'ListNode', k: 'int') -> 'ListNode':
def reverse_group(pre_first, first):
# initialize pointers
n_pre = first
n = n_pre.next
n_post = n.next
# iterate over k nodes skipping first one
i = 1
while i < k:
n.next = n_pre
# shift pointers
n_pre = n
n = n_post
if n_post:
n_post = n_post.next
i += 1
first.next = n
if pre_first:
pre_first.next = n_pre
return n_pre, first
def check_length(first):
n = first
i = 0
while i < k:
if n is None:
return False
n = n.next
i += 1
return True
if k <= 1 or head is None:
return head
if check_length(head):
new_head, p = reverse_group(None, head)
else:
return head
while p.next is not None and check_length(p.next):
_, p = reverse_group(p, p.next)
return new_head
__________________________________________________________________________________________________