-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path143-重排链表.py
27 lines (22 loc) · 850 Bytes
/
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
class Solution:
def reorderList(self, head: ListNode) -> None:
"""
Do not return anything, modify head in-place instead.
"""
if head and head.next:
mylist = []
while head:
mylist.append(head)
head = head.next
n = len(mylist)
if n % 2 == 1:
for i in range(n // 2):
mylist[i].next = mylist[n - 1 - i]
mylist[n - 1 - i].next = mylist[i + 1]
mylist[n // 2].next = None
else:
for i in range(n // 2 - 1):
mylist[i].next = mylist[n - 1 - i]
mylist[n - 1 - i].next = mylist[i + 1]
mylist[n // 2 - 1].next = mylist[n // 2]
mylist[n // 2].next = None