Advertisement

Linked List Cycle - LeetCode 141 Solution

Linked List Cycle - Complete Solution Guide

Linked List Cycle is LeetCode problem 141, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 . Example 1: Input: head = [3,2,0,-4], pos = 1 Output:

Detailed Explanation

The problem asks you to determine if a given singly linked list contains a cycle (a loop). The input is the head of the linked list. The output is a boolean value: `true` if a cycle exists, and `false` otherwise. The problem doesn't provide the index of the node where the cycle begins (if one exists); you need to detect the cycle's presence without this information. The linked list nodes contain integer values.

Solution Approach

The provided code implements Floyd's Tortoise and Hare algorithm. It uses two pointers, `slow` and `fast`, initialized to the head of the linked list. The `slow` pointer moves one node at a time, while the `fast` pointer moves two nodes at a time. The algorithm iterates until either the `fast` pointer reaches the end of the list (indicating no cycle) or the `slow` and `fast` pointers meet (indicating a cycle).

Step-by-Step Algorithm

  1. Step 1: Initialize two pointers, `slow` and `fast`, to the head of the linked list.
  2. Step 2: Check for the base case: If the head is null or the next node is null, there is no cycle, return `false`.
  3. Step 3: Move `slow` one step forward and `fast` two steps forward in each iteration.
  4. Step 4: If `slow` and `fast` pointers meet at any point, a cycle is detected, return `true`.
  5. Step 5: If the `fast` pointer reaches the end of the list (becomes null or its next node is null), there is no cycle, return `false`.

Key Insights

  • Insight 1: Using two pointers, one moving twice as fast as the other, is crucial for detecting cycles. If a cycle exists, the fast pointer will eventually lap the slow pointer.
  • Insight 2: The Floyd's Tortoise and Hare algorithm is the most efficient approach for this problem, leveraging the two-pointer technique to achieve O(1) space complexity.
  • Insight 3: Handling edge cases such as an empty list or a list with only one node is essential to avoid null pointer exceptions or incorrect results.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Hash Table, Linked List, Two Pointers.

Companies

Asked at: Autodesk, Cisco, DE Shaw, EPAM Systems, Goldman Sachs, Intel, Oracle, Qualcomm, SAP, Samsung, Uber, Walmart Labs, Wipro, Yahoo, ZScaler.