Circular linked list
Topic Source: stay button (LeetCode)
link: https://leetcode-cn.com/problems/linked-list-cycle-ii
At present taking into account the two solutions, but they require auxiliary space, the first O (n) second O (1)
Means for determining a first auxiliary dictionary
Through the node are recorded in a dictionary, the dictionary is determined by querying whether a key value exists cycloalkyl
time complexity of O (n) , the spatial complexity is O (n)
code show as below:
# -*- coding: utf-8 -*-
# @Author : xaohuihui
# @Time : 19-12-6
# @File : detect_cycled.py
# Software : study
"""
检测环形链表
"""
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
def has_cycle(head: ListNode) -> bool:
dict_node = dict()
i = 0
if head and head.next:
while head and head.next:
id_head = str(id(head))
if dict_node.get(id_head) is None:
dict_node[id_head] = i
else:
return True
i += 1
head = head.next
return False
else:
return False
if __name__ == '__main__':
# head=[3,2,0,4] pos= 1
node1 = ListNode(3)
node2 = ListNode(2)
node3 = ListNode(0)
node4 = ListNode(4)
node1.next = node2
node2.next = node3
node3.next = node4
node4.next = node2
print(has_cycle(node1))
Output is as follows:
True
The second solution Pointer speed
# -*- coding: utf-8 -*-
# @Author : xaohuihui
# @Time : 19-12-6
# @File : detect_cycled.py
# Software : study
"""
检测环形链表
"""
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
# 第二种解法
def has_cycle(head: ListNode) -> bool:
if head and head.next:
fast = slow = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if fast == slow:
return True
else:
return False
if __name__ == '__main__':
# head=[3,2,0,4] pos= 1
node1 = ListNode(3)
node2 = ListNode(2)
node3 = ListNode(0)
node4 = ListNode(4)
node1.next = node2
node2.next = node3
node3.next = node4
node4.next = node2
print(has_cycle(node1))
Output
True