Find the Middle Node
Find the middle node of an index-based linked list in one pass with slow and fast pointers.
The problem
Implement middle_link_index(next_indices, head). A singly linked list is stored as next pointers: node i points to node next_indices[i], and -1 marks the end. The list starts at node head and has no cycle. Return the index of the middle node; when the list has an even number of nodes, return the second of the two middle nodes. For example, next_indices = [1, 2, 3, -1] with head = 0 is the list 0 -> 1 -> 2 -> 3, so the answer is 2. If head is -1 the list is empty; return -1. Find the middle in one pass, without counting the nodes first.
Starter code
def middle_link_index(next_indices, head):
passPublic examples
next_indices = [1, 2, 3, 4, -1], head = 0gives2next_indices = [1, 2, 3, -1], head = 0gives2
Solving it in the practice book needs an active paid plan.
Open it in the practice bookOne thought at a time.
