-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path033 Reverse Nodes in k-Group.py
49 lines (38 loc) · 1.06 KB
/
033 Reverse Nodes in k-Group.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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseKGroup(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
if not head:
return None
if k==1:
return head
h1 = h2 = head
n = 0
while h2:
h2 = h2.next
n += 1
l = []
ll = []
j = 0
while h1:
j += 1
i = 0
last = None
while i<k and h1:
if i==0:
l.append(h1)
i += 1
temp = h1.next
h1.next = last
h1,last = temp,h1
if last:
ll.append(last)
if j == n//k:
ll.append(h1)
break
for i in range(len(l)):
l[i].next = ll[i+1]
return ll[0]