LeetCode Solution

Nisarg Devdhar
3 min readSep 30, 2020

--

141. Linked List Cycle

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

There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter.

Return true if there is a cycle in the linked list. Otherwise, return false.

Follow up:

Can you solve it using O(1) (i.e. constant) memory?

Example 1:

Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).

Example 2:

Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.

Example 3:

Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.

Constraints:

  • The number of the nodes in the list is in the range [0, 104].
  • -105 <= Node.val <= 105
  • pos is -1 or a valid index in the linked-list.

Solution 1:(Brute-force)

The brute-force solution uses a HashSet. Assuming how a basic HashSet works according to the Collections framework. We traverse through the entire list starting from the head of the list and add every new node encountered in to the list . Incase we come into a node that has already been encountered in the list then we return false. On reaching the last node if all nodes of the list are unique true is returned.

/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
Set<ListNode> nodesSeen = new HashSet<>();
while (head != null) {
if (nodesSeen.contains(head)) {
return true;
} else {
nodesSeen.add(head);
}
head = head.next;
}
return false;
}
}

Solution 2:(two pointer method)

The optimal method being the use of two pointers. Here we use 2 pointers both starting from the head of the list. The first pointer holds the present node in the list and the second pointer holds the position of the node that is 2 nodes away from the current node. This loop iterates till the end and if there is no cycle present then returns false, else returns true.

/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
if (head == null) {
return false;
}
ListNode slow = head;
ListNode fast = head.next;
while (slow != fast) {
if (fast == null || fast.next == null) {
return false;
}
slow = slow.next;
fast = fast.next.next;
}
return true;
}
}

Note:

The time complexities of both the methods remain the same that is O(n). However, the space complexities of the two solutions vary the brute force solution will be O(n), the optimal solution returns a space complexity of O(1) which is the space consumed by the list itself.

--

--