K18

Linked List Cycle Detection

Junior Level Candidate
Target Time: 15 - 20 Minutes
Type: BOOLEAN

Problem Statement & Objective

Given head, the head of a linked list, determine if the linked list has a cycle in it (Floyd's Tortoise and Hare algorithm).

Function Signature (Python)

has_cycle(nodes: list[list[int]], head: int) -> bool

Input & Output Contract

Output Rule:True: bas'tan başlayan yürüyüş bir düğüme yeniden girer. False: zincir -1 ile biter. Erişilmeyen döngü False'tur.

System Constraints

0 <= dugumler.length <= 10^4
-10^4 <= değer <= 10^4
bas, geçerli bir indeks veya -1
sonraki_indeks, -1 veya 0..n-1 aralığındadır

Example Execution Walkthrough

dugumler = [[3,1],[2,2],[0,3],[-4,1]], bas = 0
0→1→2→3→1 döngüsü. Cevap True.

dugumler = [[1,1],[2,-1],[9,3],[8,2]], bas = 0
Baştan giden zincir 0→1→son. İndeks 2 ve 3 kendi aralarında döner ama baştan erişilmez.
Cevap False. Tüm sonraki alanlarını tarayan çözüm bunu yanlışlıkla True sayar.

Example Test Scenarios (3)

Example 1: Cycle Exists
Input:[[[3,1],[2,2],[0,3],[-4,1]],0]
Expected Output:true
Example 2: İki Düğümlü Döngü
Input:[[[1,1],[2,0]],0]
Expected Output:true
Example 3: No Cycle
Input:[[[1,-1]],0]
Expected Output:false
Loading Python Editor...
Input Parameters
[
  [
    [
      3,
      1
    ],
    [
      2,
      2
    ],
    [
      0,
      3
    ],
    [
      -4,
      1
    ]
  ],
  0
]
Expected Output
true