Re: [閒聊] 每日leetcode

看板Marginalman作者 (caster )時間1年前 (2024/03/07 12:46), 編輯推噓0(001)
留言1則, 1人參與, 1年前最新討論串22/1548 (看更多)
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

03/07 13:00, 1年前 , 1F
大師 幾ms的那種參考就好
03/07 13:00, 1F
文章代碼(AID): #1bwKQHnU (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1bwKQHnU (Marginalman)