BackhardTwo Pointers

Cycle Detector in Node Sequences Solution

Problem Statement

Given a sequence of nodes, determine whether the sequence contains a cycle. A cycle is defined as a sequence of nodes that, when traversed, eventually returns to a previous node. Implement an efficient algorithm to detect the presence of a cycle in the sequence.

Example 1
Input
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5]
Output
True

Explanation: The sequence contains a cycle, as node 5 is revisited after node 10.

Example 2
Input
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
Output
False

Explanation: The sequence does not contain a cycle, as each node is unique and not revisited.

Constraints

  • The sequence is represented as a list of node values.
  • Each node value is an integer between 1 and 1000.
  • The sequence contains at most 1000 nodes.
  • The algorithm should have a time complexity of O(n) and a reasonable space complexity.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free