Detect the First Node of the Loop in a Singly Linked List

You are provided with a singly linked list that may contain a cycle. Your task is to return the node where the cycle begins, if such a cycle exists.

A cycle occurs if a node's next pointer points back to any preceding node in the list. This means the linked list will not have a distinct start and end but will cycle through a set of nodes.

Input:

The first line contains an integer ‘T’ that signifies the number of test cases. Each test case includes: The first line containing singly linked list elements, separated by a single space and ending with -1. Note that -1 is a terminator and not a list element. The second line includes an integer "pos" that indicates the 0-indexed position in the list where the tail connects back to form a cycle. If "pos" is -1, the linked list has no cycle.

Output:

For each test case, print the integer "pos" which indicates the 0-indexed position of the first node in the linked list that forms the cycle's start. Print -1 if there is no cycle in the linked list.

Example:

Input:
3
1 2 3 4 5 -1
-1
1 2 3 4 5 6 7 8 -1
2
1 -1
0
Output:
-1
2
0

Constraints:

  • 0 ≤ T ≤ 50
  • -104 ≤ N ≤ 104
  • -1 ≤ pos < N
  • -109 ≤ data ≤ 109 and data ≠ -1
  • Time Limit: 1 sec

Note:

Simply implement the function; output handling is predefined.

Follow-Up:

Can you solve this problem using O(N) time complexity and constant space?
AnswerBot
4mo

To detect the first node of the loop in a singly linked list, we can use Floyd's Cycle Detection Algorithm.

  • Use Floyd's Cycle Detection Algorithm to find the meeting point of the slow and fast pointers...read more

Help your peers!
Select
Add answer anonymously...

Microsoft Corporation Software Developer interview questions & answers

A Software Developer was asked 8mo agoQ. Given an integer array nums and an integer k, return the kth largest element in ...read more
A Software Developer was asked 8mo agoQ. Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0'...read more
A Software Developer was asked 9mo agoQ. Why do you want to join Microsoft?

Popular interview questions of Software Developer

A Software Developer was asked 8mo agoQ1. Given an integer array nums and an integer k, return the kth largest element in ...read more
A Software Developer was asked 8mo agoQ2. Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0'...read more
A Software Developer was asked 9mo agoQ3. Why do you want to join Microsoft?
Microsoft Corporation 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