All topics
45

Splicing a node in and out of a chain

Tier 1Locked

Where it runs

1. The problem

This is what a linked list gives up access by index for. In an array, inserting at position 2 shifts every later item one cell along, and deleting shifts them back. Both cost O(n) copies. In a linked list, inserting in the middle changes exactly two links, and deleting changes one, however long the chain is. In this lesson you write these two operations: put a node at position i, and take the node at position i out. Cheap edits like these are the reason people use linked lists.

This step is part of the paid track

The first 33 steps are open to everyone: lesson, exercise and tests. This one opens with full access. Start with a free account and the free steps.

Create a free accountSee what is included

On LeetCode

Problems that insert or delete nodes in a linked list, at the head, in the middle, or wherever a value matches. Find the node before the spot, then rewire one or two next pointers.

We use necessary cookies to run the site (sign-in and language). If you accept, we also load Google Analytics to see which pages are used, and Google reCAPTCHA to keep spam off the contact and bug-report forms. Privacy policy