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