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.

Be the first one to answer
Add answer anonymously...
Capgemini Engineering Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter