AcWing 34. 链表中环的入口结点-python
原题链接
中等
作者:
acwing_827
,
2019-09-04 11:58:18
,
所有人可见
,
阅读 961
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def entryNodeOfLoop(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head:
return None
#进入环内
h = head.next
t = head
while h and t and h!=t:
if not h.next:
return None
h = h.next.next
t = t.next
if not h or not t:
return None
#算环长
k=1
h = h.next
while h!=t:
h = h.next
k+=1
# print(k)
#找入口
h, t= head, head
for i in range(k):
h = h.next
while h!=t:
h = h.next
t = t.next
return h