-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path143.py
67 lines (59 loc) · 1.6 KB
/
143.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
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def reorderList(self, head):
"""
:type head: ListNode
:rtype: void Do not return anything, modify head in-place instead.
"""
if head == None or head.next == None or head.next.next == None:
return
dummy_1, dummy_2 = ListNode(0), ListNode(0)
dummy_1.next = head
temp_head = head
length = 0
while head != None:
head = head.next
length += 1
half = length//2
head = temp_head
for i in range(half):
head = head.next
dummy_2.next = head.next
head.next = None
cur, nxt = None, dummy_2.next
tmp = nxt.next
while True:
nxt.next = cur
cur = nxt
nxt = tmp
if tmp == None:
break
tmp = tmp.next
dummy_2.next = cur
temp_head_1, temp_head_2 = dummy_1.next, dummy_2.next
tmp1, tmp2 = temp_head_1.next, temp_head_2.next
while True:
temp_head_1.next = temp_head_2
temp_head_2.next = tmp1
if tmp1 == None or tmp2 == None:
break
temp_head_1 = tmp1
temp_head_2 = tmp2
tmp1 = tmp1.next
tmp2 = tmp2.next
head = dummy_1.next
a = ListNode(1)
b = ListNode(2)
c = ListNode(3)
d = ListNode(4)
e = ListNode(5)
a.next = b
b.next = c
c.next = d
# d.next = e
node1 = Solution().reorderList(a)
print(node1)