Grind 75 Python 做題記錄 876. Middle of the Linked List
Grind 75 Python 做題記錄 876. Middle of the Linked List
# 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]:
# 建立快慢針
fast = slow = head
# 快針遍歷,快針結束,慢針過半
while fast and fast.next:
# 慢針進1
slow = slow.next
# 快針進2
fast = fast.next.next
# 回傳過半慢針
return slow