-
Notifications
You must be signed in to change notification settings - Fork 0
/
025ReverseNodesinKGroup.py
60 lines (50 loc) · 1.65 KB
/
025ReverseNodesinKGroup.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
#After practice, I got similar or better idea than the previous one
# 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:
if not head or not head.next or k==1:
return head
jump=dummy_node=ListNode(0)
dummy_node.next=head
while True:
start=end=jump.next
if not end:
return dummy_node.next
for _ in range(k-1):
end=end.next
if not end:
return dummy_node.next
#reverse start to end
pre = end.next
cur=start
for i in range(k):
cur.next, cur, pre= pre, cur.next, cur
jump.next= pre
jump=start
#Solution from https://leetcode.com/problems/reverse-nodes-in-k-group/discuss/11491/Succinct-iterative-Python-O(n)-time-O(1)-space
#How can this guy so smart
class Solution(object):
def reverseKGroup(self, head, k):
"""
:type head: ListNode
:type k: int
:rtype: ListNode
"""
prehead= jump= ListNode(-1)
prehead.next= l= r = head
while True:
count=0
while r and count<k:
r= r.next
count+=1
if count==k:
pre, curr= r, l
for _ in range(k):
curr.next, curr, pre = pre, curr.next, curr
jump.next, jump, l= pre, l, r
else:
return prehead.next