-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode_0092.py
60 lines (46 loc) · 1.49 KB
/
leetcode_0092.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
from typing import Optional
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
if right <= left: # no need to change anything
return head
tail = None # after right
rev_mid = None # between left and right
head_new = None # before left
# navigate on linked list
counter: int = 1 # to compare element pos with left and right
current = head
while current:
if counter >= left and counter <= right:
new_node = ListNode(current.val, rev_mid)
if not rev_mid:
tail = new_node
rev_mid = new_node
elif counter > right:
tail.next = current
break
elif counter == left - 1:
head_new = current
current = current.next
counter += 1
if head_new:
head_new.next = rev_mid
return head
return rev_mid # rev_mid include reverse part + tail
# region linked list
# [1,2,3,4,5]
head = ListNode(1)
curr = head
for n in range(2, 6):
curr.next = ListNode(n)
curr = curr.next
# endregion
s = Solution()
result = s.reverseBetween(head, left=2, right=4)
while result:
print(result.val, end=", ")
result = result.next