问题描述:返回中间节点。链表的()

题目链接: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):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        slow,fast = head,head
        while(fast and fast.next):
            fast = fast.next.next
            slow = slow.next
        
        return slow

Leetcode 876. Middle of the Linked List

相关文章: