Step 08 Linked-List — Fundamentals (Single LL)
Insertion in Linked List
Insertion is the mirror image of deletion: create a node, then wire it into the chain in the right order so nothing after the insertion point is lost. This lesson covers the four standard cases — at the head, at the tail, at position k, and before a given value — and the one ordering rule that prevents the most common linked-list bug.
The golden rule of insertion order
When splicing a new node between A and B, always point the new node at B first, and only then point A at the new node.
If you rewire A first (A.next = new_node) before saving B, the reference to B — and the entire rest of the list — is gone. Every insertion case below is just: find the splice point, then apply 'new node points forward first, predecessor points at new node second'.
Case 1: insert at head, Case 2: insert at tail
At the head: the new node's next is the old head, and the new node becomes head. O(1), and it works on an empty list with no special casing (old head is None, which is exactly the right next).
At the tail: if the list is empty, the new node is the head. Otherwise walk to the last node (temp.next is None) and attach the new node there. O(n) for the walk.
def insert_head(head, x):
return Node(x, head) # new node -> old head
def insert_tail(head, x):
new_node = Node(x)
if head is None:
return new_node
temp = head
while temp.next is not None:
temp = temp.next
temp.next = new_node
return headCase 3: insert at position k
Positions are 1-indexed: inserting at position k means the new node becomes the k-th node, and the node previously at k shifts to k+1, so you splice after the (k-1)-th node. If k == 1, it is the head case. If k is beyond length+1, most problem statements treat it as invalid or as an append — read the statement.
Walk with a counter to position k-1, then apply the golden rule: new_node.next = temp.next; temp.next = new_node.
def insert_at_k(head, x, k):
new_node = Node(x)
if k == 1:
new_node.next = head
return new_node
temp = head
count = 1
while temp is not None and count < k - 1:
temp = temp.next
count += 1
if temp is None: # k out of range
return head
new_node.next = temp.next # 1) point forward first
temp.next = new_node # 2) then link predecessor
return headCase 4: insert before a value X
Walk until you find the node whose next holds X — you must stop one node early, because the splice happens at the predecessor. If X is the head's value, it is the head case. If X is absent, the list is left unchanged.
All four insertion cases are O(n) worst case (except head insertion, O(1)) and O(1) extra space besides the new node itself.
def insert_before_value(head, x, val):
# insert val just before the first node containing x
if head is None:
return None
if head.data == x:
return Node(val, head)
temp = head
while temp.next is not None and temp.next.data != x:
temp = temp.next
if temp.next is not None:
temp.next = Node(val, temp.next)
return headCommon mistakes
(1) Rewiring the predecessor before setting the new node's next — the golden-rule violation that loses the tail of the list. (2) Off-by-one on position k: remember you stop at k-1 because the new node takes position k. (3) Forgetting the empty-list and head-value special cases in the tail and before-value routines. (4) Not returning head from every insert function — inserting at the head changes head just like deleting it did.
Key points
- Golden rule: set the new node's next first, then link the predecessor to it — never the other way round.
- Insert at head is O(1); tail, position-k and before-value insertions walk first, so O(n).
- Position k insertion splices after node k-1; k == 1 is the head case.
- Before-value insertion must stop at the predecessor of the target node.
- Every insert routine returns the (possibly new) head.