-
Notifications
You must be signed in to change notification settings - Fork 0
/
143_reorderList.py
168 lines (120 loc) · 3.33 KB
/
143_reorderList.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# 143. Reorder List
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
# split list in half, reverse 2nd list and then merge lists.
def reverseList(head):
prev = None
while head:
next_node = head.next
head.next = prev
prev = head
head = next_node
return prev
def mergeLists(l1, l2):
p1 = l1
p2 = l2
while p1 and p2:
temp = p1.next
p1.next = ListNode(p2.val)
p1.next.next = temp
p1 = p1.next.next
p2 = p2.next
return l1
class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
"""
Do not return anything, modify head in-place instead.
"""
l = 0
p1 = head
while p1:
l+=1
p1 = p1.next
import math
half = math.ceil(l/2)
p2 = head
while half>1:
p2 = p2.next
half-=1
list2 = p2.next
p2.next = None
list2 = reverseList(list2)
head = mergeLists(head, list2)
'''
if not head.next or not head.next.next:
return head
ptr1 = head
ptr2 = head.next.next
ptr3 = head
stack = []
l = 1
while ptr2:
stack.append(ptr2.val)
ptr2 = ptr2.next
l+=1
l+=1
import copy
l1 = copy.deepcopy(l)
while ptr1.next:
temp = ptr1.next
ptr1.next = ListNode(stack.pop())
ptr1.next.next = temp
l-=2
if l<=1:
break
ptr1 = ptr1.next.next
while(l1>0):
ptr3 = ptr3.next
l1-=1
if l1==1:
ptr3.next = None
'''
'''
p1 = head
l = 0
while p1:
l+=1
p1 = p1.next
stack = []
import math
inds, inds1 = l-(math.ceil(l/2)-1), l-(math.ceil(l/2)-1)
p1 = head
while inds>0:
inds-=1
p1 = p1.next
while p1:
stack.append(p1.val)
p1 = p1.next
p1 = head
while stack:
val = ListNode(stack.pop())
temp = p1.next
p1.next = val
val.next = temp
p1 = p1.next.next
p1 = head
while l>1:
l-=1
p1 = p1.next
p1.next = None
return head
'''
# BRUTE FORCE
# while ptr1:
# while ptr2.next.next:
# ptr2 = ptr2.next
# temp = ptr1.next
# ptr1.next = ListNode(ptr2.next.val)
# ptr1.next.next = temp
# ptr2.next = None
# ptr1 = ptr1.next.next
# if ptr1.next:
# ptr2 = ptr1.next
# else:
# break
# if not ptr2.next:
# break
return head