Leetcode 876. Middle of the Linked List

扫两遍链表.

# 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:
        if not head.next:return head
        num = 1
        node = head
        while node.next:
            num += 1
            node = node.next
        mid = num // 2
        now = 0
        node = head
        while True:
            if now == mid:
                return node
            now += 1
            node=node.next
        
原文地址:https://www.cnblogs.com/zywscq/p/10721238.html