1609. 链表的中间结点
1609. 链表的中间结点
中文English
给定一个带有头结点 head 的非空单链表,返回链表的中间结点。
如果有两个中间结点,则返回第二个中间结点。
样例
样例 1:
输入:1->2->3->4->5->null
输出:3->4->5->null
样例 2:
输入:1->2->3->4->5->6->null
输出:4->5->6->null
注意事项
The number of nodes in the given list will be between 1 and 100
快慢指针
""" Definition of ListNode class ListNode(object): def __init__(self, val, next=None): self.val = val self.next = next """ class Solution: """ @param head: the head node @return: the middle node """ def middleNode(self, head): # write your code here. #快慢指针 if not head: return None quick, slow = head, head while quick and quick.next: slow = slow.next quick = quick.next.next return slow