{
  "id": "linked-lists/circular-linked-list",
  "version": 1,
  "deterministic": true,
  "url": "/dsa/linked-lists/circular-linked-list",
  "topic": {
    "slug": "linked-lists",
    "title": "Linked Lists"
  },
  "title": "Circular Linked List",
  "tagline": "The tail points back at the head. There is no end — and that's the point.",
  "vizKind": "list",
  "complexity": {
    "time": {
      "best": "O(1) insert at head",
      "average": "O(n) to find",
      "worst": "O(n) full lap"
    },
    "space": "O(n)",
    "growth": "linear",
    "note": "The same costs as a singly linked list — the ring doesn't make anything faster. What it changes is reachability: from any node you can reach every node, and traversal never naturally ends."
  },
  "code": {
    "javascript": "// The tail points back at the head — there is no NULL.\nfunction buildCircular(values) {\n  let head = null, tail = null;\n  for (const v of values) {\n    const node = new Node(v);\n    if (!head) head = node;\n    else tail.next = node;\n    tail = node;\n  }\n  tail.next = head;            // close the loop\n  return head;\n}\n\n// Careful: this loops forever without a stopping rule.\nfunction walkOnce(head) {\n  let cur = head;\n  do {\n    visit(cur);\n    cur = cur.next;\n  } while (cur !== head);      // stop when we come home\n}",
    "python": "# The tail points back at the head — there is no None.\ndef build_circular(values):\n    head = tail = None\n    for v in values:\n        node = Node(v)\n        if head is None: head = node\n        else: tail.next = node\n        tail = node\n\n    tail.next = head             # close the loop\n    return head\n\n\n# Careful: this loops forever without a stopping rule.\ndef walk_once(head):\n    cur = head\n    while True:\n        visit(cur)\n        cur = cur.next\n        if cur is head: break    # stop when we come home\n",
    "java": "// The tail points back at the head — there is no null.\nNode buildCircular(int[] values) {\n  Node head = null, tail = null;\n  for (int v : values) {\n    Node node = new Node(v);\n    if (head == null) head = node;\n    else tail.next = node;\n    tail = node;\n  }\n  tail.next = head;            // close the loop\n  return head;\n}\n\n// Careful: this loops forever without a stopping rule.\nvoid walkOnce(Node head) {\n  Node cur = head;\n  do {\n    visit(cur);\n    cur = cur.next;\n  } while (cur != head);       // stop when we come home\n}",
    "cpp": "// The tail points back at the head — there is no nullptr.\nNode* buildCircular(vector<int>& values) {\n  Node *head = nullptr, *tail = nullptr;\n  for (int v : values) {\n    Node* node = new Node(v);\n    if (!head) head = node;\n    else tail->next = node;\n    tail = node;\n  }\n  tail->next = head;           // close the loop\n  return head;\n}\n\n// Careful: this loops forever without a stopping rule.\nvoid walkOnce(Node* head) {\n  Node* cur = head;\n  do {\n    visit(cur);\n    cur = cur->next;\n  } while (cur != head);       // stop when we come home\n}"
  },
  "defaultInput": [
    4,
    8,
    15,
    16
  ],
  "defaultTarget": null,
  "inputHint": "The chain closes into a ring, then we walk two full laps.",
  "pitfalls": [
    {
      "wrong": "Looping with `while (cur !== null)`.",
      "right": "There is no null — it will never stop. Stop when you return to the head: `do { ... } while (cur !== head)`."
    },
    {
      "wrong": "Forgetting to close the loop after building the list.",
      "right": "If the tail still points at NULL, you've just built an ordinary linked list. The final `tail.next = head` is what makes it circular."
    },
    {
      "wrong": "Creating a ring by accident.",
      "right": "A bug that links a node back into the list turns every traversal into an infinite hang. Use Floyd's fast/slow pointers to detect a cycle — that's what LeetCode's 'Linked List Cycle' is really about."
    }
  ],
  "quiz": [
    {
      "q": "How do you know when to stop walking a circular linked list?",
      "options": [
        "When you reach NULL",
        "When you arrive back at the head",
        "After n steps, counted separately",
        "You can't — it never stops"
      ],
      "answer": 1,
      "why": "There is no NULL to find. You stop when the walk brings you home to the node you started from — that's exactly one full lap."
    },
    {
      "q": "What can you do from any node in a circular list that you can't in an ordinary one?",
      "options": [
        "Delete it faster",
        "Reach every other node, including the ones 'behind' you",
        "Sort the list",
        "Find its index"
      ],
      "answer": 1,
      "why": "In an ordinary list, anything behind your current position is gone forever. In a ring, keep walking and you'll come back around to it."
    },
    {
      "q": "Why is an *accidental* circular link so dangerous?",
      "options": [
        "It uses too much memory",
        "It turns any normal traversal into an infinite loop that hangs silently",
        "It reverses the list",
        "It's not dangerous"
      ],
      "answer": 1,
      "why": "Code that expects to reach NULL will loop forever with no error. That's why cycle detection — Floyd's fast and slow pointers — is such a common interview question."
    }
  ],
  "problems": [
    {
      "name": "Linked List Cycle",
      "difficulty": "Easy",
      "url": "https://leetcode.com/problems/linked-list-cycle/"
    },
    {
      "name": "Linked List Cycle II",
      "difficulty": "Medium",
      "url": "https://leetcode.com/problems/linked-list-cycle-ii/"
    },
    {
      "name": "Insert into a Sorted Circular Linked List",
      "difficulty": "Medium",
      "url": "https://leetcode.com/problems/insert-into-a-sorted-circular-linked-list/"
    }
  ],
  "trace": {
    "note": "Each frame is one immutable step: data snapshot, pointers, per-index roles, executing code line, and a one-sentence explanation.",
    "length": 15,
    "frames": [
      {
        "data": {
          "nodes": [],
          "circular": false
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 2,
        "variables": {
          "length": 0
        },
        "explanation": "A circular linked list has no end. The last node doesn't point at NULL — it points back at the first one, closing the chain into a ring.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "c0-4",
              "value": 4
            }
          ],
          "circular": false
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "swap"
          }
        ],
        "codeLine": 7,
        "variables": {
          "added": 4,
          "length": 1
        },
        "explanation": "Link 4 onto the end. For now the chain still finishes at NULL.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "c0-4",
              "value": 4
            },
            {
              "id": "c1-8",
              "value": 8
            }
          ],
          "circular": false
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "swap"
          }
        ],
        "codeLine": 7,
        "variables": {
          "added": 8,
          "length": 2
        },
        "explanation": "Link 8 onto the end. For now the chain still finishes at NULL.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "c0-4",
              "value": 4
            },
            {
              "id": "c1-8",
              "value": 8
            },
            {
              "id": "c2-15",
              "value": 15
            }
          ],
          "circular": false
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "swap"
          }
        ],
        "codeLine": 7,
        "variables": {
          "added": 15,
          "length": 3
        },
        "explanation": "Link 15 onto the end. For now the chain still finishes at NULL.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "c0-4",
              "value": 4
            },
            {
              "id": "c1-8",
              "value": 8
            },
            {
              "id": "c2-15",
              "value": 15
            },
            {
              "id": "c3-16",
              "value": 16
            }
          ],
          "circular": false
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 3,
            "role": "swap"
          }
        ],
        "codeLine": 7,
        "variables": {
          "added": 16,
          "length": 4
        },
        "explanation": "Link 16 onto the end. For now the chain still finishes at NULL.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "c0-4",
              "value": 4
            },
            {
              "id": "c1-8",
              "value": 8
            },
            {
              "id": "c2-15",
              "value": 15
            },
            {
              "id": "c3-16",
              "value": 16
            }
          ],
          "circular": true
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 3,
            "role": "found"
          },
          {
            "index": 0,
            "role": "found"
          }
        ],
        "codeLine": 11,
        "variables": {
          "length": 4
        },
        "explanation": "Now close the loop: point the tail (16) back at the head (4). There is no NULL anywhere in this structure any more.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "c0-4",
              "value": 4
            },
            {
              "id": "c1-8",
              "value": 8
            },
            {
              "id": "c2-15",
              "value": 15
            },
            {
              "id": "c3-16",
              "value": 16
            }
          ],
          "circular": true
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          },
          {
            "name": "cur",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "active"
          }
        ],
        "codeLine": 19,
        "variables": {
          "lap": 1,
          "at": 4,
          "steps": 1
        },
        "explanation": "Visiting 4 (lap 1). Keep following next…",
        "stats": {
          "comparisons": 1,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "c0-4",
              "value": 4
            },
            {
              "id": "c1-8",
              "value": 8
            },
            {
              "id": "c2-15",
              "value": 15
            },
            {
              "id": "c3-16",
              "value": 16
            }
          ],
          "circular": true
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          },
          {
            "name": "cur",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "active"
          }
        ],
        "codeLine": 19,
        "variables": {
          "lap": 1,
          "at": 8,
          "steps": 2
        },
        "explanation": "Visiting 8 (lap 1). Keep following next…",
        "stats": {
          "comparisons": 2,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "c0-4",
              "value": 4
            },
            {
              "id": "c1-8",
              "value": 8
            },
            {
              "id": "c2-15",
              "value": 15
            },
            {
              "id": "c3-16",
              "value": 16
            }
          ],
          "circular": true
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          },
          {
            "name": "cur",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "active"
          }
        ],
        "codeLine": 19,
        "variables": {
          "lap": 1,
          "at": 15,
          "steps": 3
        },
        "explanation": "Visiting 15 (lap 1). Keep following next…",
        "stats": {
          "comparisons": 3,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "c0-4",
              "value": 4
            },
            {
              "id": "c1-8",
              "value": 8
            },
            {
              "id": "c2-15",
              "value": 15
            },
            {
              "id": "c3-16",
              "value": 16
            }
          ],
          "circular": true
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          },
          {
            "name": "cur",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 3,
            "role": "active"
          }
        ],
        "codeLine": 19,
        "variables": {
          "lap": 1,
          "at": 16,
          "steps": 4
        },
        "explanation": "Visiting 16 (lap 1). Keep following next…",
        "stats": {
          "comparisons": 4,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "c0-4",
              "value": 4
            },
            {
              "id": "c1-8",
              "value": 8
            },
            {
              "id": "c2-15",
              "value": 15
            },
            {
              "id": "c3-16",
              "value": 16
            }
          ],
          "circular": true
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          },
          {
            "name": "cur",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "active"
          }
        ],
        "codeLine": 19,
        "variables": {
          "lap": 2,
          "at": 4,
          "steps": 5
        },
        "explanation": "We're back at the head — that's one full lap. This is the only way to know when to stop: compare against the head, not against NULL.",
        "stats": {
          "comparisons": 5,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "c0-4",
              "value": 4
            },
            {
              "id": "c1-8",
              "value": 8
            },
            {
              "id": "c2-15",
              "value": 15
            },
            {
              "id": "c3-16",
              "value": 16
            }
          ],
          "circular": true
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          },
          {
            "name": "cur",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "active"
          }
        ],
        "codeLine": 19,
        "variables": {
          "lap": 2,
          "at": 8,
          "steps": 6
        },
        "explanation": "Visiting 8 (lap 2). Keep following next…",
        "stats": {
          "comparisons": 6,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "c0-4",
              "value": 4
            },
            {
              "id": "c1-8",
              "value": 8
            },
            {
              "id": "c2-15",
              "value": 15
            },
            {
              "id": "c3-16",
              "value": 16
            }
          ],
          "circular": true
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          },
          {
            "name": "cur",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "active"
          }
        ],
        "codeLine": 19,
        "variables": {
          "lap": 2,
          "at": 15,
          "steps": 7
        },
        "explanation": "Visiting 15 (lap 2). Keep following next…",
        "stats": {
          "comparisons": 7,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "c0-4",
              "value": 4
            },
            {
              "id": "c1-8",
              "value": 8
            },
            {
              "id": "c2-15",
              "value": 15
            },
            {
              "id": "c3-16",
              "value": 16
            }
          ],
          "circular": true
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          },
          {
            "name": "cur",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 3,
            "role": "active"
          }
        ],
        "codeLine": 19,
        "variables": {
          "lap": 2,
          "at": 16,
          "steps": 8
        },
        "explanation": "Visiting 16 (lap 2). Keep following next…",
        "stats": {
          "comparisons": 8,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "c0-4",
              "value": 4
            },
            {
              "id": "c1-8",
              "value": 8
            },
            {
              "id": "c2-15",
              "value": 15
            },
            {
              "id": "c3-16",
              "value": 16
            }
          ],
          "circular": true
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          }
        ],
        "codeLine": 21,
        "variables": {
          "length": 4
        },
        "explanation": "Left alone it would go round forever — which is exactly why it's used for things that *should* go round forever: turn-taking in a game, round-robin CPU scheduling, a repeating playlist.",
        "stats": {
          "comparisons": 8,
          "swaps": 0
        }
      }
    ]
  }
}