- # Definition for singly-linked list.# class ListNode(object):# def __init__(self, val=0, next=None):# self.val = val# self.next = next
class Solution(object):
def middleNode(self, head):
"""
:type head: Optional[ListNode]
:rtype: Optional[ListNode]"""
if not head or not head.next:
# 若链表不存在或只有一个节点时,返回headreturnhead
fast, slow = head, headwhile fast and fast.next:
fast = fast.next.next
slow = slow.next
return slow