-
Notifications
You must be signed in to change notification settings - Fork 21
/
ReverseLinkedListII.py
52 lines (42 loc) · 1.1 KB
/
ReverseLinkedListII.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
# Reverse a linked list from position m to n. Do it in-place and in one-pass.
#
# For example:
# Given 1->2->3->4->5->NULL, m = 2 and n = 4,
#
# return 1->4->3->2->5->NULL.
#
# Note:
# Given m, n satisfy the following condition:
# 1 ≤ m ≤ n ≤ length of list.
#
# Python, Python3 all accepted.
class ReverseLinkedListII:
def reverseBetween(self, head, m, n):
"""
:type head: ListNode
:type m: int
:type n: int
:rtype: ListNode
"""
node = head
lst = []
i = 0
while i <= n - 1 and node is not None:
if m - 1 <= i:
lst.append(node)
node = node.next
i += 1
while len(lst) >= 2:
tmp = lst[0].val
size = len(lst)
lst[0].val = lst[size - 1].val
lst[size - 1].val = tmp
del lst[0]
del lst[size - 2]
return head
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
def __eq__(self, other):
return self.val == other.val and self.next == other.next