Detect and Remove a Linked List Loop
Updated
Problem statement
A singly linked list is stored in an array next. The nodes are numbered from 0 to next.length - 1. Node i points to node next[i], and next[i] = -1 means node i has no next node. The list starts at node head.
The list may contain a loop: when you follow the pointers from head, you may come back to a node you have already visited, and the walk never ends. Find out whether the list has a loop. If it does, remove the loop by changing one pointer to -1: the pointer of the last new node you reach, which is the pointer that leads back to an already visited node. After that, the list visits the same nodes in the same order and then ends.
Return the array next after the change. If the list has no loop, return it unchanged. Nodes that cannot be reached from head are not part of the list, and their entries must stay as they are.
Function
removeLoop(next: int[], head: int) → int[]Examples
Example 1
next = [3,5,5,1,-1,0]head = 2return = [3,-1,5,1,-1,0]Starting at node 2, the walk visits nodes 2, 5, 0, 3, 1. Node 1 points to node 5, which was already visited, so nodes 5, 0, 3, 1 form the loop. Setting next[1] = -1 removes it. Node 4 cannot be reached from node 2, so its entry stays -1.
Example 2
next = [-1,0,1]head = 2return = [-1,0,1]The walk visits nodes 2, 1, 0 and stops, because next[0] = -1. There is no loop, so the array is returned unchanged.
Example 3
next = [1,1]head = 0return = [1,-1]The walk visits nodes 0, 1, and node 1 points to itself. The loop is node 1 alone, so next[1] becomes -1.
Constraints
1 <= next.length <= 2000-1 <= next[i] < next.length0 <= head < next.length
More Bank of America Onsite Interview questions
- Centered Star PyramidONSITE INTERVIEW · Seen Nov 2025
- Employees Without a ManagerONSITE INTERVIEW · Seen Nov 2025
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2025
- N-th Fibonacci NumberONSITE INTERVIEW · Seen Nov 2025
- Reverse Each Word in a SentenceONSITE INTERVIEW · Seen Nov 2025
- Second Highest SalaryONSITE INTERVIEW · Seen Nov 2025
- Three Numbers That Add Up to a TargetONSITE INTERVIEW · Seen Apr 2025
- Duplicate RowsONSITE INTERVIEW · Seen Oct 2023
- Factorial of a NumberONSITE INTERVIEW · Seen Oct 2023
- Palindrome String CheckONSITE INTERVIEW · Seen Sep 2023
Similar questions at other companies
- Detect a Linked-List CyclePaytm · ONSITE INTERVIEWPaytm interview questions
- Detect a Linked-List CycleCapgemini · ONSITE INTERVIEWCapgemini interview questions
- Detect a Linked-List CycleMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Find a Linked-List Cycle EntryExpedia · ONSITE INTERVIEWExpedia interview questions
- First Node Two Linked Lists ShareAdobe · PHONE SCREENAdobe interview questions
- Where Two Linked Lists MeetNutanix · PHONE SCREENNutanix interview questions
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions
- Shortest Window with Required CountsOracle · ONSITE INTERVIEWOracle interview questions
- LRU CacheLotusFlare · ONSITE INTERVIEW