---
title: "Linked list problems, worked"
url: "https://georgeliinternships.com/interview-prep/coding/linked-lists.md"
canonical_url: "https://georgeliinternships.com/interview-prep/coding/linked-lists"
last_updated: "2026-12-01"
type: "guide"
summary: "Linked lists for coding interviews: node variants, Big-O costs, dummy nodes, two-pointer patterns, edge cases and four checked practice problems."
related: ["https://georgeliinternships.com/interview-prep.md", "https://georgeliinternships.com/methodology.md", "https://georgeliinternships.com/roles.md"]
---

# Linked list problems for coding interviews: patterns and practice

> **For AI agents:** the complete index of George Li Internships pages is [llms.txt](https://georgeliinternships.com/llms.txt). Every page is also served as markdown at its URL plus .md.

Linked lists for coding interviews: node variants, Big-O costs, dummy nodes, two-pointer patterns, edge cases and four checked practice problems.

Linked lists appear often in coding interviews because pointer updates test how cleanly you reason about memory and boundaries. This guide covers memory layout, list variants, Big-O costs, core pointer techniques, edge cases, and practice problems.

## How linked lists work and compare with arrays

Arrays and linked lists both store ordered sequences, yet their memory layouts differ completely. Arrays allocate one contiguous memory block so elements sit side by side. Linked lists place nodes anywhere in memory and connect them through stored addresses.

Every basic node stores two fields: a data value and a reference pointing to the next node in order.

- Advantage: inserting or deleting at a known node runs in O(1) time because you update references rather than shifting later elements like an array.
- Disadvantage: indexing takes O(n) time. You cannot jump straight to arr[4]; you must step forward from the head.

Pair short readings with university lectures on singly and doubly linked lists if you want a visual refresher on pointer memory before coding.

## Three structural variants you will see

### Singly linked lists

Each node holds a value and one next reference, while the final node points to null. Most interview prompts use this layout.

### Doubly linked lists

Each node keeps both next and prev pointers. The first node sets prev to null, and the last node sets next to null, enabling backward traversal and O(1) deletion when given the node.

### Circular linked lists

In a circular singly linked list, the final node points back to the head instead of null. In a circular doubly linked list, the head’s prev points to the tail and the tail’s next points to the head.

## Language support and Big-O operation costs

Among major languages, Java provides LinkedList and C++ provides std::list, while Python and JavaScript have no built-in linked list type. Writing a custom Node class takes only a few lines in any language, and interviewers usually supply one.

| Language | Standard library | Notes |
| --- | --- | --- |
| C++ | std::list | Doubly linked container; problems often use Node structs |
| Java | java.util.LinkedList | Built-in doubly linked list; interviews pass ListNode |
| Python | None | Define a simple Node class with val and next |
| JavaScript | None | Define a Node class or object with val and next |
*Built-in linked list containers by language*

Watch the assumption behind insertion and removal costs: O(1) applies only after you already hold a reference to that position.

| Operation | Big-O | Assumption |
| --- | --- | --- |
| Access | O(n) | Walk sequentially from head |
| Search | O(n) | Inspect nodes until matched |
| Insert | O(1) | Pointer already sits at target spot |
| Remove | O(1) | Pointer already sits at node or predecessor |
*Time complexity of core linked list operations*

Copying results into a fresh linked list simplifies many tasks but uses O(n) extra space. Interviewers normally expect in-place pointer updates with O(1) auxiliary space, often adapting mechanics from reversing a list.

## Core pointer techniques and modification tricks

### Sentinel and dummy nodes

Placing a dummy sentinel before the head, after the tail, or at both ends avoids null-check branches when editing list boundaries. Because real nodes never sit at the outer edge during updates, edge logic stays uniform. Return sentinel.next and drop the dummy node when finished.

### Two-pointer patterns

- Kth node from the end: advance one pointer k steps ahead, then move both pointers together until the leader finishes.
- Cycle detection: move a fast pointer two steps per turn and a slow pointer one step. If they meet, a cycle exists.
- Middle node: advance a fast pointer at double speed; when it reaches the end, the slow pointer rests at the midpoint.

### Clean in-place modifications

- Truncate a list: set the new ending node’s next pointer to null.
- Swap two items: exchange node values directly when object identity does not matter, avoiding pointer rewiring.
- Combine two lists: attach the second list’s head onto the first list’s tail.

> **Tip.** Confirm with your interviewer before mutating node values; some problems require relinking actual nodes rather than swapping payloads.

## Common subroutines and corner cases

Most interview solutions assemble four foundational routines:

1. Counting total nodes in one pass.
2. Reversing pointers in place using previous, current, and next variables.
3. Locating the midpoint with slow and fast pointers.
4. Merging two separate chains node by node.

Always dry-run four edge cases before finishing:

- Empty list where head is null.
- Single-node list.
- Two-node list, which exposes off-by-one loop errors.
- Cyclic lists. Ask your interviewer upfront whether cycles are allowed; usually input is acyclic so cycle checks are unnecessary.

## Essential problems and how to practice

Focus on recognizing reusable pointer patterns rather than memorizing solutions. Master these core drills first:

- Reverse a singly linked list iteratively and recursively.
- Detect a cycle in O(1) space.
- Merge two sorted lists with a dummy starter node.
- Delete the nth node from the tail in one pass.

Once those feel automatic, practice multi-step problems:

- Reorder a list by splitting at the midpoint, reversing the second half, and interleaving nodes.
- Merge k sorted lists using divide-and-conquer or a heap.
- Deep-copy a list with random pointers or check palindrome symmetry in O(1) extra space.

> Save the next pointer before overwriting a link, and trace null, one-node, and two-node inputs every time.

## Four practice problems, in order of difficulty

These are our own practice problems. Try each on paper first, then open the working. They run from a single pass with a gap to a pointer reversal in groups. These are our own practice problems, written for this site. If you have signed an agreement about an assessment, keep its contents to yourself, including in a coaching session.

**Problem 1.** A singly linked list holds the values 14, 8, 21, 5, 17, 9, 30, 12, 6, 25, 3 from head to tail. The 3rd node from the tail is removed, then the whole list is reversed. What value is in the 4th node from the head after both steps? Give an integer.

**Answer:** 30

1. To find the 3rd node from the tail in one pass, start two pointers at the head and move the first one 3 steps ahead. Then move both until the first falls off the end. The second pointer stops just before the node to remove, so you can unlink it.
2. The list has 11 nodes, so the 3rd from the tail is the 9th from the head, the value 6. After removal the list is 14, 8, 21, 5, 17, 9, 30, 12, 25, 3.
3. Reversing walks the list once, pointing each node back at the one before it. The list becomes 3, 25, 12, 30, 9, 17, 5, 21, 8, 14.
4. The 4th node from the head holds 30. Removal and reversal each take one pass, O(n) time, and reversal uses O(1) extra space because it only rewires pointers.

**Problem 2.** Two sorted singly linked lists hold 1, 4, 5, 9, 12, 13 and 2, 3, 6, 7, 15, 16, 17, 20. They are merged into one sorted list by repeatedly comparing the two front nodes and moving the smaller one to the output, taking the node from the first list when the two are equal. When one list runs out, the rest of the other list is attached with no further comparisons. How many comparisons are made? Give an integer.

**Answer:** 10

1. Each comparison moves exactly one node to the output, so the number of comparisons equals the number of nodes moved before one list is empty.
2. The first list empties when its last node, 13, is moved. The nodes moved up to that point are 1, 2, 3, 4, 5, 6, 7, 9, 12 and 13, in that order.
3. That is all 6 nodes of the first list and the 4 nodes of the second list that are smaller than 13, which are 2, 3, 6 and 7. So 6 + 4 = 10 comparisons.
4. The tail 15, 16, 17, 20 is attached by changing one pointer, with no comparisons. Merging two lists of lengths m and n takes at most m + n comparisons, and it needs no extra nodes because it reuses the ones it has.

**Problem 3.** Twelve nodes are numbered 0 to 11. The next pointers of nodes 0, 1, 2 and so on up to 11 are 5, 7, 9, 10, 6, 2, 4, 3, -1, 1, 7, 0, in that order, and -1 means there is no next node. Starting at node 0 and following next pointers, the walk eventually repeats a node. How many nodes are on the cycle it enters? Give an integer.

**Answer:** 3

1. Use two pointers that start at node 0. The slow one moves one node per step and the fast one moves two nodes per step. If there is a cycle they must meet inside it, and if the list ended the fast one would reach -1.
2. The walk from node 0 goes 0, 5, 2, 9, 1, 7, 3, 10, and then back to 7. The slow pointer visits 5, 2, 9, 1, 7, 3 over six steps, while the fast pointer visits 2, 1, 3, 7, 10, 3. On the sixth step both are at node 3.
3. Leave one pointer at node 3 and move the other around until they meet again: 3 to 10, 10 to 7, 7 to 3. That is 3 steps, so the cycle has 3 nodes: 7, 3 and 10.
4. Nodes 4, 6, 8 and 11 are not reached from node 0, so they do not matter. The method uses O(n) time and O(1) extra space, where a set of visited nodes would use O(n) space.

**Problem 4.** A singly linked list holds 7, 2, 9, 4, 11, 6, 1, 8, 5, 10 from head to tail. The list is cut into consecutive groups of 3 nodes starting at the head, and each full group is reversed in place. A last group with fewer than 3 nodes is left as it is. What is the sum of the values at positions 1, 3, 5, 7 and 9 of the new list, counting the head as position 1? Give an integer.

**Answer:** 33

1. Handle one group at a time. For each group, check that 3 nodes remain, reverse those 3 by pointing each node at the one before it, then connect the group to the one before and the one after.
2. The groups are 7, 2, 9 then 4, 11, 6 then 1, 8, 5 then 10. The three full groups become 9, 2, 7 and 6, 11, 4 and 5, 8, 1. The last group has only 1 node and stays.
3. The new list is 9, 2, 7, 6, 11, 4, 5, 8, 1, 10. Positions 1, 3, 5, 7 and 9 hold 9, 7, 11, 5 and 1.
4. Their sum is 9 + 7 + 11 + 5 + 1 = 33. The pass is O(n) time and O(1) extra space. The usual mistake is to reverse the short last group, which this problem rules out on purpose.

Every answer above is recomputed by a separate program when the site is built, and the program rebuilds each result by a different route from the written steps. A wrong answer would stop the build.

## Questions

### What is the time cost of a singly linked list?

Open Data Structures shows a singly linked list pushing, popping, adding and removing at the head or the tail in constant time, except removing from the tail, which needs a walk from the head. Reaching a node by position takes a walk through the list.

### How do you detect a cycle in a linked list?

Move one pointer one step at a time and another two steps at a time. If there is a cycle they meet inside it, and if the fast pointer reaches the end there is none. The method uses constant extra space.

### Why use a dummy head node?

A dummy node placed before the head means the head is no longer a special case when you insert or remove nodes, so the same code handles every position.

## How a session helps

A session with George is a place to work a problem aloud and have the reasoning questioned. You write every line of code and every answer yourself. An hour is $250, and a package of 5 sessions is $1,165 for a full interview season. Your first purchase is refundable for unused time within 7 days. Next, read the [data structures checklist](https://georgeliinternships.com/interview-prep/data-structures-checklist.md) and the [coding and complexity practice](https://georgeliinternships.com/interview-prep/coding-and-complexity-practice.md), and use the [coding prep schedule](https://georgeliinternships.com/tools/coding-prep-schedule.md) to space your practice. The [college students page](https://georgeliinternships.com/for/college-students.md) and the [packages page](https://georgeliinternships.com/packages.md) list the sessions.

## Related

- [Internship interview prep](https://georgeliinternships.com/interview-prep.md)
- [Methodology and sources](https://georgeliinternships.com/methodology.md)
- [Internship role guides](https://georgeliinternships.com/roles.md)
