forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0092.py
More file actions
35 lines (27 loc) · 711 Bytes
/
0092.py
File metadata and controls
35 lines (27 loc) · 711 Bytes
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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:
if not head:
return None
prev = None
curr = head
for _ in range(m - 1):
prev = curr
curr = curr.next
conn = prev
tail = curr
for _ in range(n - m + 1):
next = curr.next
curr.next = prev
prev = curr
curr = next
if conn:
conn.next = prev
else:
head = prev
tail.next = curr
return head