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 includedOn 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.
Delete every node with that value by pointing prev.next past it, the head included.
Equal values sit next to each other, so skip a node whenever it repeats the one before.
addAtIndex and deleteAtIndex are this lesson's insertAt and deleteAt under other names.
Find the node before a and the node after b, then splice all of list2 between them.