-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path92.py
125 lines (96 loc) · 2.97 KB
/
92.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def create_linked_list(arr):
head = ListNode(arr[0])
cur = head
for i in range(1, len(arr)):
cur.next = ListNode(arr[i])
cur = cur.next
return head
def print_linked_list(head):
cur = head
res = []
while cur:
res.append(cur.val)
cur = cur.next
return res
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseAreaList(self, head: ListNode, left: int, right: int) -> ListNode:
# dummy = ListNode(0, head)
# first = head
# second = dummy
# for i in range(n):
# first = first.next
# while first:
# first = first.next
# second = second.next
# second.next = second.next.next
# return dummy.next
p = head
left -= 1
p_left = None
right -= 1
p_right = None
left_low = left -1
p_left_low = None
right_high = right + 1
p_right_high = None
for i in range(right_high):
if i == left -1:
p_left_low = p
if i== right + 1:
p_right_high = p
if i >= left:
q = p
p = p.next
r = p.next
p.next = q
p_left_low.next = p_right
p_left
return head
def reverseAreaList2(self, head: ListNode, left: int, right: int):
def reverseAreaList_innrt(root):
if root.next is None:
return root
last = reverseAreaList_innrt(root.next)
root.next.next = root
root.next = None
return last
p = head
len_head = 0
while p.next:
p = p.next
len_head += 1
head_need_left = head
for _ in range(left):
if _ == 0:
continue
head_need_left = head_need_left.next
last_right = reverseAreaList_innrt(head_need_left.next)
head_need_left.next = last_right
# print(print_linked_list(head))
head_need_right = last_right
for _ in range(len_head-right):
if _ == 0:
continue
head_need_right = head_need_right.next
last_left = reverseAreaList_innrt(head_need_right.next)
head_need_right.next = last_left
# print(print_linked_list(head))
last = reverseAreaList_innrt(last_right)
head_need_left.next = last
# print(print_linked_list(head))
return head
if __name__ == "__main__":
solu = Solution()
head = create_linked_list([1,5,8,4,5])
new_head = solu.reverseAreaList2(head,2,3)
print(print_linked_list(new_head))