-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path143.py
More file actions
29 lines (25 loc) · 1012 Bytes
/
143.py
File metadata and controls
29 lines (25 loc) · 1012 Bytes
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
# 這題是給一個 link list,然後把整串對折,形成 1->n->2->n-1->3->n-2->... 的順序
# 做法就是用 fast slow two pointer 先找中間點,然後從中間點開始做 list 的反轉
# 反轉完之後就按照順序逐個連接好就可以了!
# 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: return
fast = slow = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
prev, cur = None, slow
while cur:
cur.next, prev, cur = prev, cur, cur.next
first, second = head, prev
while second.next:
first.next, first = second, first.next
second.next, second = first, second.next