Re: [閒聊] 每日leetcode

作者: sustainer123 (caster)   2024-03-07 12:46:05
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%的解法一模一樣
作者: NCKUEECS (小惠我婆)   2024-03-07 13:00:00
大師 幾ms的那種參考就好

Links booklink

Contact Us: admin [ a t ] ucptt.com