Linked List Cycle Detection

Determine if a given singly linked list of integers forms a cycle.

Explanation:

A cycle in a linked list occurs when a node's next reference points back to a previous node in the list, transforming the structure from a linear layout into a loop.

Input:
The first line contains the elements of the singly linked list separated by a space, ending with -1 to indicate termination. 
The second line provides an integer "pos", the position (0-indexed) where the tail of the linked list connects back in the list. A "pos" value of -1 indicates there is no cycle.
Output:
Return 'true' if the linked list contains a cycle, otherwise return 'false'. The output handling is managed internally.
Example:
Input:
3 2 0 -4 -1
1
Output:
true

Constraints:

  • 0 <= N <= 10^6
  • -1 <= pos < N
  • -10^9 <= data <= 10^9 and data ≠ -1

Note: Attempt to solve this problem with O(N) time complexity and O(1) space complexity.

AnswerBot
4mo

Detect if a singly linked list forms a cycle by checking if a node's next reference points back to a previous node.

  • Use two pointers, one moving at double the speed of the other, to detect a cycle.

  • If t...read more

Help your peers!
Select
Add answer anonymously...

Snapdeal Software Developer interview questions & answers

A Software Developer was asked Q. Given two linked lists that merge at a point, find the merging point.
A Software Developer was asked Q. Given an M x N matrix, how many rectangles are there?
A Software Developer was asked Q. Find a four-digit number in the form aabb that is a perfect square.

Popular interview questions of Software Developer

A Software Developer was asked Q1. Given two linked lists that merge at a point, find the merging point.
A Software Developer was asked Q2. Given an M x N matrix, how many rectangles are there?
A Software Developer was asked Q3. Find a four-digit number in the form aabb that is a perfect square.
Snapdeal Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits