Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Cycle Detection in Linked Lists

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Cycle Detection in Linked Lists. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Given the head of a singly linked list, determine whether the list contains a cycle. A cycle exists if a node can be reached again by continuously following next pointers.

Return True if the linked list has a cycle, otherwise return False.

Formal Specification

  • Input: head, the head node of a singly linked list. Each node has fields val and next.
  • Output: A boolean value indicating whether the list contains a cycle.

You must solve the problem without modifying the linked list structure.

Constraints

  • 0 <= number of nodes <= 10^5
  • -10^5 <= Node.val <= 10^5
  • pos is -1 or a valid index
  • Do not modify the linked list

Function Signature

def has_cycle(head):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output