Skip to content

Commit 260b655

Browse files
Merge pull request neetcode-gh#2933 from coopers/0025-python
Update 0025-reverse-nodes-in-k-group.py
2 parents a13306a + 2d17af7 commit 260b655

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

python/0025-reverse-nodes-in-k-group.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
class Solution:
22
def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
3-
dummy = ListNode(0, head)
4-
groupPrev = dummy
5-
3+
dummy = groupPrev = ListNode(0, head)
4+
65
while True:
76
kth = self.getKth(groupPrev, k)
87
if not kth:
98
break
9+
groupPrev.next = kth
1010
groupNext = kth.next
1111

1212
# reverse group
13-
prev, curr = kth.next, groupPrev.next
13+
prev, curr = groupNext, head
1414
while curr != groupNext:
15-
tmp = curr.next
15+
nxt = curr.next
1616
curr.next = prev
1717
prev = curr
18-
curr = tmp
18+
curr = nxt
1919

20-
tmp = groupPrev.next
21-
groupPrev.next = kth
22-
groupPrev = tmp
20+
groupPrev = head
21+
head = groupNext
2322
return dummy.next
2423

2524
def getKth(self, curr, k):

0 commit comments

Comments
 (0)