Skip to content
CampusEduX

Detect Cycle in Linked List

Mediumjavapythonccppjavascript

A singly linked list of N nodes (indices 0 .. N-1) is described. The tail's next pointer either points at the node with index pos, forming a cycle, or is null (given as pos = -1). Determine whether the list contains a cycle.

Input Format

The first line contains an integer N. The second line contains N space-separated integers, the node values (empty if N is 0). The third line contains an integer pos: the index the tail links back to, or -1 for no cycle.

Output Format

Print 1 if the list contains a cycle, otherwise print 0.

Related Problems

  • Add Two Numbers Using Linked Lists
    Medium · java, python, c, cpp, javascript
    Solve Problem
  • Intersection Point of Two Linked Lists
    Medium · java, python, c, cpp, javascript
    Solve Problem
  • Remove Cycle from Linked List
    Medium · java, python, c, cpp, javascript
    Solve Problem

Input (stdin)

Output

Run your code to see output here...