-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy path2074-reverse-nodes-in-even-length-groups.py
51 lines (45 loc) · 1.47 KB
/
2074-reverse-nodes-in-even-length-groups.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
# time complexity: O(n)
# space complexity: O(1)
from typing import Optional
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def reverseEvenLengthGroups(self, head: Optional[ListNode]) -> Optional[ListNode]:
prev = head
groupLength = 2
while prev.next:
node = prev
n = 0
for _ in range(groupLength):
if node.next is None:
break
node = node.next
n += 1
if n % 2:
prev = node
else:
reverse = node.next
curr = prev.next
for _ in range(n):
currNext = curr.next
curr.next = reverse
reverse = curr
curr = currNext
prevNext = prev.next
prev.next = node
prev = prevNext
groupLength += 1
return head
head = ListNode(5)
head.next = ListNode(2)
head.next.next = ListNode(6)
head.next.next.next = ListNode(3)
head.next.next.next.next = ListNode(9)
head.next.next.next.next.next = ListNode(1)
head.next.next.next.next.next.next = ListNode(7)
head.next.next.next.next.next.next.next = ListNode(3)
head.next.next.next.next.next.next.next.next = ListNode(8)
head.next.next.next.next.next.next.next.next.next = ListNode(4)
print(Solution().reverseEvenLengthGroups(head))