-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0025.py
More file actions
28 lines (26 loc) · 846 Bytes
/
0025.py
File metadata and controls
28 lines (26 loc) · 846 Bytes
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
# 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:
dummy = ListNode(0)
dummy.next = head
start = dummy
while start.next:
end = start
for i in range(k-1):
end = end.next
if end.next == None: return dummy.next
(start.next, start) = self.reverseList(start.next, end.next)
return dummy.next
def reverseList(self, start, end):
dummy = ListNode(0)
dummy.next = start
while dummy.next != end:
tmp = start.next
start.next = tmp.next
tmp.next = dummy.next
dummy.next = tmp
return (end, start)