bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 141. Linked List Cycle

141. Linked List Cycle

Leetcode Solutions

Floyd's Cycle Finding Algorithm

  1. Initialize two pointers, slow and fast, both pointing to the head of the linked list.
  2. Move slow pointer by one step and fast pointer by two steps in each iteration.
  3. If the fast pointer reaches the end of the list (i.e., fast is null or fast.next is null), there is no cycle, and return false.
  4. If the slow pointer and the fast pointer meet (i.e., slow == fast), a cycle is detected, and return true.
  5. Repeat steps 2-4 until either a cycle is detected or the end of the list is reached.
UML Thumbnail

Hash Table Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...