876 Middle of the Linked List

快慢指针方法找中点

Python

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def middleNode(self, head: 'ListNode') -> 'ListNode':
        fast = slow = head
        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next
        return slow

results matching ""

    No results matching ""