-
Notifications
You must be signed in to change notification settings - Fork 2
/
q0143.py
41 lines (34 loc) · 966 Bytes
/
q0143.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
#!/usr/bin/python3
from typing import List
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def reorderList(self, head: ListNode) -> None:
"""
Do not return anything, modify head in-place instead.
"""
if head is None:
return
nodesList: List[ListNode] = list()
p: ListNode = head
while p is not None:
nodesList.append(p)
p = p.next
lenOfList: int = len(nodesList)
for i in range(lenOfList // 2):
nodesList[i].next = nodesList[lenOfList - i - 1]
nodesList[lenOfList - i - 1].next = nodesList[i + 1]
nodesList[len(nodesList) // 2].next = None
node1 = ListNode(1)
node2 = ListNode(2)
node3 = ListNode(3)
node1.next = node2
node2.next = node3
solu = Solution()
solu.reorderList(node1)
p = node1
while p is not None:
print(p.val)
p = p.next