forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
36 lines (32 loc) · 1011 Bytes
/
Solution.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
# 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: ListNode) -> None:
"""
Do not return anything, modify head in-place instead.
"""
if head is None or head.next is None:
return
# 快慢指针找到链表中点
slow, fast = head, head.next
while fast and fast.next:
slow, fast = slow.next, fast.next.next
# cur 指向右半部分链表
cur = slow.next
slow.next = None
# 反转右半部分链表
pre = None
while cur:
t = cur.next
cur.next = pre
pre, cur = cur, t
cur = head
# 此时 cur, pre 分别指向链表左右两半的第一个节点
while pre:
t = pre.next
pre.next = cur.next
cur.next = pre
cur, pre = pre.next, t