其他都一般
今晚的这个环形链表属实牛犇
快慢指针(追击问题)+数学推导
贴上卡哥的讲解和数学证明
把环形链表讲清楚! 如何判断环形链表?如何找到环形链表的入口? LeetCode:142.环形链表II_哔哩哔哩_bilibili
有收获!
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def detectCycle(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
slow = fast = head
while slow and fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
index1 = head
index2 = fast
while index1 != index2:
index1 = index1.next
index2 = index2.next
return index1