You are given the head
of a singly linked-list. The list can be represented as:
Reorder the list to be on the following form:
You may not modify the values in the list's nodes. Only nodes themselves may be changed.
Example 1:
Example 2:
Constraints:
Node.val
<= 1000這題一樣是先用 two pointers 找到 middle node,然後紀錄 middle node 以後的 nodes,全部丟到 stack 裡面,之後一個一個 pop 出來 append 回 list 就會是 reorder 的順序了
所以程式碼第 11 到 16 行在找 middle node,第 18 到 24 行在把後半部的 nodes 丟到 stack 裡面,而有個小細節是在 stack 裡面的 nodes next
都會是空的,這是因為等等處理 next
的問題比較簡單,畢竟在 stack 裡面的 nodes 都要連回前半部的 nodes,這件事情我在後面一定會做,所以希望這邊不要亂只到我不想要的地方
reorder 的部分也很容易,每一輪都去 stack pop 一個 element 回來連上去就好,這就是程式碼第 26 到 34 行在做的事情
第 35 到 36 行是這次的重點,會因為 list 長度奇偶影響這一部執行與否,但結果都是我要讓最後一個 element 的 next
是 None,這樣 list 才不會變成 cycle,也才是正確的 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.
"""
# Get the middle of list ( -> slow )
fast = head
slow = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
# Push last half into stack
stack = []
while slow:
tmp = slow.next
slow.next = None
stack.append(slow)
slow = tmp
# Reorder the list
slow = head # Since there is no use in slow, I'll use slow as a pointer instead of renew another pointer
bk = head
while stack:
bk = slow.next
slow.next = stack.pop()
slow = slow.next
slow.next = bk
slow = slow.next
if slow:
slow.next = None
使用 two pointers 找到 middle nodes 花了
空間部分因為沒有額外花的記憶體,只有一些小參數而已,所以會是