Linked List Cycle
Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.
(链表有环)
Follow up: Can you solve it using O(1) (i.e. constant) memory?
Example:
1. 哈希表
使用哈希表存储已经遍历过的结点,时间复杂度为 O(n),空间复杂度为 O(n)。具体实现过程如下:
1 | # Definition for singly-linked list. |
2. 双指针
使用两个指针 slow 和 fast,在遍历链表的过程中,slow 移动一步,fast 移动两步。若存在环,两者一定会在某处相遇(在两者都进入环之后,fast一定会追赶上slow)。时间复杂度为 O(n),空间复杂度为 O(1)。具体实现过程如下:
1 | # Definition for singly-linked list. |