Linked Lists · Linked List Fundamentals

Singly Linked List Basics

Rewiring next-pointers in place — the foundational linked-list manipulation.

  • 4 min read
  • 13 practice questions
  • 3 graded problems
On this page
  1. Description
  2. The approach
  3. A second flavor: deleting a node without the head
  4. Where you'll see it
  5. Which tool for which job?
  6. Common mistakes
  7. Check yourself
  8. Key takeaways
  9. Quick revise
  10. Practice

Description

A singly linked list is a chain of nodes. Each node holds a value and a reference to the next node. Its strength is that inserting, deleting and reversing only rewire a few references, O(1) at a node you already hold, with no shifting as in an array. Its weakness is that reaching position i takes i steps. This skill makes the rewiring safe: save what you're about to overwrite, and know where the head ends up.

We'll go from simple to more technical:

  1. The pattern: reversing a list with prev, curr and next.
  2. A second flavor: deleting a node when you don't have the head.
  3. Where linked structures run in production (LRU caches, allocators, queues).
  4. The rewiring bugs, and when a linked list is the wrong choice.

The approach

To reverse a list, turn every arrow around. Walk the list with curr, and keep prev, the start of the part you've already reversed. At each node, first save curr.next, because you're about to overwrite it. Then point curr back at prev. Finally move both prev and curr one step forward.

Reversing 4 -> 7 -> 9:

StepSaved nextReversed so far (starts at prev)Still to do (starts at curr)
Start(empty)4 -> 7 -> 9
1747 -> 9
297 -> 49
3none9 -> 7 -> 4(empty)

When curr runs off the end, prev is the new head, 9.

Java

ListNode prev = null;
ListNode curr = head;
while (curr != null) {
    ListNode next = curr.next;   // save before overwriting
    curr.next = prev;            // rewire
    prev = curr;
    curr = next;
}

Python

prev = None
curr = head
while curr is not None:
    nxt = curr.next          # save before overwriting
    curr.next = prev         # rewire
    prev = curr
    curr = nxt

The rule that matters most: save curr.next before you overwrite it. Many linked-list bugs that aren't null-pointer errors break exactly this rule.

A second flavor: deleting a node without the head

Sometimes you're given a node, not the head of its list. The usual way to delete it, telling the node before it to skip it, is impossible: a singly linked list can't be walked backward. But the node still has a next. So copy the next node's value into this node, then unlink the next node:

Java

node.val = node.next.val;
node.next = node.next.next;

Python

node.val = node.next.val
node.next = node.next.next

In 4 -> 7 -> 9, deleting the node holding 7 copies 9 into it and unlinks the old 9 node, leaving 4 -> 9. Read from the head, the list looks as if 7 were removed. That's fine as long as nothing outside the list holds on to particular nodes; the deep dive covers when it does. The trick can't remove the last node: there's nothing after it to copy.

Where you'll see it

An LRU cache keeps its eviction order in a linked list (see the Object-Oriented Design course). So do memory allocators that track free blocks, and many event queues. Each one is this same careful rewiring.

Which tool for which job?

You needUse
Insert/remove at a node you already holda linked list - O(1) rewiring
Access by indexan array (ArrayList, Python list)
Add and remove at both endsArrayDeque (Java), collections.deque (Python)
Edits that may touch the heada dummy node before the head
Reverse a long listiteratively - recursion runs out of stack
Delete a node with no access to the one beforecopy the next value in (not for the last node)

Common mistakes

MistakeWhat happensDo this instead
Overwriting curr.next before saving itthe rest of the list is lostsave next first
Returning head after reversingreturns the old head, now the tailreturn prev
Special-casing the head everywhereduplicated, buggy branchesstart from a dummy node
The value-copy trick on the last nodethere's nothing to copyit can't be done without the previous node
list.get(i) in a loop over a Java LinkedListO(n^2)iterate with an iterator / for-each

Check yourself

Try to answer before opening each one - recalling beats re-reading.

1. Why save curr.next in a variable before rewiring it?

Show answer

curr.next is about to point back at prev. If you haven't saved its old value, nothing else refers to the rest of the list, and every node after curr is lost for good.

2. After the loop, why is prev the new head and not head?

Show answer

When the loop ends, curr is null and prev sits on what used to be the last node, which now comes first. head still points at the old first node, which is now the last. On a one-node list both are the same, which is why a quick test can miss this bug.

3. Why can't the value-copy trick delete the last node?

Show answer

It works by copying the next node's value in and unlinking the next node. The last node has no next node. And the other way, unlinking it from the node before it, needs exactly the node you can't reach.

Key takeaways

  • Save a node's next before you overwrite it.
  • After reversing, prev is the new head. The old head is now the tail.
  • Without the previous node, you can still delete a node by copying its successor in, but not the last node.
  • That trick assumes nothing outside the list cares which node object holds which value.
  • Choose a linked list when inserting and removing at known positions is the main need, not by default.

Quick revise

The whole skill on one page, for the night before an interview.

Everything from the lesson and the deep dive on one page. Skim it before practice or an interview, and open the full lesson for anything that doesn't ring a bell.

Pick the tool

You needUse
O(1) splice at a held nodea linked list
Index accessan array list
Both endsArrayDeque / collections.deque
Edits near the heada dummy node
Long listsiterative, not recursive

Snippets worth knowing by heart

Java

ListNode prev = null, curr = head;
while (curr != null) {
    ListNode next = curr.next;   // save before overwriting
    curr.next = prev;
    prev = curr;
    curr = next;
}
return prev;                     // the new head

Python

dummy = ListNode(0, head)        # no special case for the head
cur = dummy
while cur.next:
    if cur.next.val == target:
        cur.next = cur.next.next
    else:
        cur = cur.next
return dummy.next

The traps

  • Overwriting next before saving it: the rest of the list is lost. Save next first.
  • Returning head after reversing: that's the old head, now the tail. Return prev.
  • Head special cases: duplicated branches breed bugs. Start from a dummy node.
  • The value-copy trick on the last node: there's no next value to copy. You need the previous node.
  • get(i) loops on a LinkedList: each call walks from the head, so the loop is O(n^2). Use an iterator.

Staff-level points, one line each

  • Reversal invariant: prev + curr hold every node once; return prev.
  • A dummy node removed every 7 from 7, 7, 1, 7, 2, 7 in one loop.
  • Recursive reversal of 5,000 nodes raised RecursionError in Python.
  • get(i) loop over a 50,000-node LinkedList: over 50x slower than for-each.
  • list.insert(0, x) 100,000 times: over 10x slower than deque.appendleft.

Say it in an interview

  • "I save next before I overwrite it, so I never lose the rest of the list."
  • "After the loop, prev is the new head."
  • "A dummy node lets me treat the head like every other node."
  • "I'd reverse iteratively - recursion would need O(n) stack."

Practice

This skill comes with 13 multiple-choice questions that tell you which misconception a wrong answer reveals and 3 graded problems. Your answers are graded and your mastery is tracked, so you'll know when the skill holds up. Running code and saving progress need a free account.

Practice Singly Linked List Basics free

Delete Node in a Linked List

EasyFreeCoding problemJava · Python

You're handed one node of a singly linked list and nothing else: no head, and no way to walk back. The node is never the last one. Remove it from the list. There's nothing to return; change the list so that node's value no longer appears where it was.

Practice Singly Linked List Basics free

No card needed. Lessons, practice questions and easy problems are free forever.

Free plan, no card · 7-day Pro trial · Cancel anytime