Skip to contentKEEP THE CRAFTTHE SANCTUARY

Unlink One Node from a Linked List

Skip one node of an index-based linked list by pointing the node before it at the node after it.

The problem

Implement bypass_linked_node(next_indices, remove_index). A singly linked list is stored as a list of next pointers: node i points to node next_indices[i], and -1 marks the end. Node 0 is the head. Return a new list of next pointers in which the node before remove_index points to the node after it, so the list skips remove_index. For example, [1, 2, 3, -1] with remove_index 2 gives [1, 3, 3, -1]. Leave every other entry as it is, including the removed node's own entry, and do not modify next_indices. Nodes are not always stored in list order: [2, -1, 1] is the list 0 -> 2 -> 1. remove_index is always a node of the list. If remove_index is 0, nothing points to the head, so return an unchanged copy.

Starter code

def bypass_linked_node(next_indices, remove_index):
    pass

Public examples

  1. next_indices = [1, 2, 3, -1], remove_index = 2gives[1, 3, 3, -1]
  2. next_indices = [1, 2, -1], remove_index = 2gives[1, -1, -1]

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.