Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Linked List Cycle

Easy
linked-listtwo-pointers

Determine whether a linked list contains a cycle — i.e. some node can be reached again by following next pointers. Use O(1) memory.

Input

  • Line 1: the list values in order
  • Line 2: integer pos — the tail connects to the node at this 0-based index (-1 means no cycle). Your function receives only the head; the cycle is already wired.

Output: true or false.

Example 1
Input:
3 2 0 -4
1
Output:
true

Tail connects back to value 2.

Example 2
Input:
1 2
0
Output:
true
Example 3
Input:
1
-1
Output:
false

Constraints

  • 0 ≤ list length ≤ 10^4
  • pos is -1 or a valid index.
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.