-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path143. Reorder-List
58 lines (50 loc) · 1.66 KB
/
143. Reorder-List
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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
"""
Do not return anything, modify head in-place instead.
"""
if not head or head.next is None:
return head
def reverseSequence(node):
if not node or node.next is None:
return node
current = node
prev = None
while current:
nextNode = current.next
current.next = prev
prev = current
current = nextNode
return prev
dummyNode = ListNode()
dummyNode.next = head
slowNode = dummyNode
fastNode = dummyNode
while fastNode.next and fastNode.next.next:
fastNode = fastNode.next.next
slowNode = slowNode.next
sequence1 = sequence2 = dummyNode.next
if fastNode.next:
sequence2 = slowNode.next.next
slowNode.next.next = None
else:
sequence2 = slowNode.next
slowNode.next = None
sequence2 = reverseSequence(sequence2)
res = dummyNode
while sequence2:
nextSeq1 = sequence1.next
nextSeq2 = sequence2.next
res.next = sequence1
sequence1.next = sequence2
res = sequence2
sequence1 = nextSeq1
sequence2 = nextSeq2
if sequence1:
res.next = sequence1
return