This website contains ALL LeetCode **Premium** problems for
**FREE!!**.

All leaked interview problems are collected from Internet.

All leaked interview problems are collected from Internet.

Given a linked list, determine if it has a cycle in it.

Follow up:

Can you solve it without using extra space?

b'

\n## Summary

\n## Solution

\n

\n#### Approach #1 (Hash Table) [Accepted]

\n\n

\n#### Approach #2 (Two Pointers) [Accepted]

\n\n

'
\n\n

\nThis article is for beginners. It introduces the following ideas: Linked List, Hash Table and Two Pointers.

\n\n

**Intuition**

To detect if a list is cyclic, we can check whether a node had been visited before. A natural way is to use a hash table.

\n**Algorithm**

We go through each node one by one and record each node\'s reference (or memory address) in a hash table. If the current node is `null`

, we have reached the end of the list and it must not be cyclic. If current node\xe2\x80\x99s reference is in the hash table, then return true.

public boolean hasCycle(ListNode head) {\n Set<ListNode> nodesSeen = new HashSet<>();\n while (head != null) {\n if (nodesSeen.contains(head)) {\n return true;\n } else {\n nodesSeen.add(head);\n }\n head = head.next;\n }\n return false;\n}\n

**Complexity analysis**

- \n
- \n
Time complexity : .\nWe visit each of the elements in the list at most once. Adding a node to the hash table costs only time.

\n \n - \n
Space complexity: .\nThe space depends on the number of elements added to the hash table, which contains at most elements.

\n \n

\n

**Intuition**

Imagine two runners running on a track at different speed. What happens when the track is actually a circle?

\n**Algorithm**

The space complexity can be reduced to by considering two pointers at **different speed** - a slow pointer and a fast pointer. The slow pointer moves one step at a time while the fast pointer moves two steps at a time.

If there is no cycle in the list, the fast pointer will eventually reach the end and we can return false in this case.

\nNow consider a cyclic list and imagine the slow and fast pointers are two runners racing around a circle track. The fast runner will eventually meet the slow runner. Why? Consider this case (we name it case A) - The fast runner is just one step behind the slow runner. In the next iteration, they both increment one and two steps respectively and meet each other.

\nHow about other cases? For example, we have not considered cases where the fast runner is two or three steps behind the slow runner yet. This is simple, because in the next or next\'s next iteration, this case will be reduced to case A mentioned above.

\npublic boolean hasCycle(ListNode head) {\n if (head == null || head.next == null) {\n return false;\n }\n ListNode slow = head;\n ListNode fast = head.next;\n while (slow != fast) {\n if (fast == null || fast.next == null) {\n return false;\n }\n slow = slow.next;\n fast = fast.next.next;\n }\n return true;\n}\n

**Complexity analysis**

- \n
- \n
Time complexity : .\nLet us denote as the total number of nodes in the linked list. To analyze its time complexity, we consider the following two cases separately.

\n- \n
- \n

\n*List has no cycle:*

\nThe fast pointer reaches the end first and the run time depends on the list\'s length, which is . \n - \n

\n*List has a cycle:*

\nWe break down the movement of the slow pointer into two steps, the non-cyclic part and the cyclic part:- \n
- \n
The slow pointer takes "non-cyclic length" steps to enter the cycle. At this point, the fast pointer has already reached the cycle. \n

\n \n - \n
Both pointers are now in the cycle. Consider two runners running in a cycle - the fast runner moves 2 steps while the slow runner moves 1 steps at a time. Since the speed difference is 1, it takes loops for the fast runner to catch up with the slow runner. As the distance is at most "cyclic length K" and the speed difference is 1, we conclude that \n

\n \n

\n - \n

Therefore, the worst case time complexity is , which is .

\n \n - \n
- \n
Space complexity : .\nWe only use two nodes (slow and fast) so the space complexity is .

\n \n

Analysis written by: @tianyi8, revised by @1337c0d3r.

\n