题目描述
给定一个链表,若其中包含环,则输出环的入口节点。
若其中不包含环,则输出null。
样例
blablabla
算法1
(暴力枚举) $O(n^2)$
时间复杂度
参考文献
Python 代码
# 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
"""
dict = {}
head1 = head
count = 0
while head1 != None:
dict[head1.val] = 0
if count > 1000:
break
count += 1
head1 = head1.next
while head != None:
dict[head.val] += 1
if dict[head.val] > 1:
return head
break
head = head.next
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla