Leetcode刷题记录——876. 链表的中间结点

在这里插入图片描述
快慢指针即可

# 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 head == None:
            return None
        if head.next == None:
            return head
        slow = head
        cur = head
        while cur != None and cur.next != None:# and  cur.next.next != None:
            slow = slow.next#k += 1
            cur = cur.next.next
        return slow

猜你喜欢

转载自blog.csdn.net/weixin_41545780/article/details/107572245