精華區beta Marginalman 關於我們 聯絡資訊
876. Middle of the Linked List 找中間值,如有兩個中間值,取第二個 Example 1: Input: head = [1,2,3,4,5] Output: [3,4,5] Explanation: The middle node of the list is node 3. Example 2: Input: head = [1,2,3,4,5,6] Output: [4,5,6] Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one. 思路: 快慢指針end Python Code: # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]: slow = head fast = head while fast and fast.next: slow = slow.next fast = fast.next.next return slow 今天75比較快刷完 多刷一題每日 不過為啥跑出來的速度才贏30% 我看我的解法跟贏100%的解法一模一樣 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.137.214.158 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1709786769.A.C5E.html
NCKUEECS: 大師 幾ms的那種參考就好 03/07 13:00