给定一个链表,判断链表中是否有环。
为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。
示例 1:
输入:head = [3,2,0,-4], pos = 1
输出:true
解释:链表中有一个环,其尾部连接到第二个节点。
示例 2:
输入:head = [1,2], pos = 0
输出:true
解释:链表中有一个环,其尾部连接到第一个节点。
示例 3:
输入:head = [1], pos = -1
输出:false
解释:链表中没有环。
进阶:
你能用 O(1)(即,常量)内存解决此问题吗?
# 这道题我的想法始终是要遍历一边链表O(n)才能发现是不是有回路.
# 不过题目不是说,在输入的时候有pos这个变量吗?那直接return pos != -1 不就行了??
# 用 O(1)(即,常量)内存解决此问题
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Solution(object):
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
# 采用双指针,p1,p2;p1为慢指针,每次走一个结点,p2为快指针,每次走两个结点
# 记录p1,p2走的结点数,当两结点相遇时,结点数之差即为环结点数
# 结点起始位置为 慢指针结点数 - 环结点数
fast = head
slow = head
while fast is not None and fast.next is not None:
fast = fast.next.next
slow = slow.next
if fast == slow:
return True
return False
# 巧用集合,判断是否存在交点
def hasCycle2(self, head):
"""
:type head: ListNode
:rtype: bool
"""
save = set()
cur = head
while cur is not None:
if cur in save:
return True
else:
save.add(cur)
cur = cur.next
return False
if __name__ == '__main__':
s = Solution()
l1 = ListNode(1)
l1.next = ListNode(1)
l1.next.next = ListNode(2)
l1.next.next.next = ListNode(2)
l1.next.next.next.next = ListNode(3)
#l1.next.next.next.next.next = l1.next.next
print(s.hasCycle(l1))