Linked List Implementation
Data Structures
Large Scale Systems
Programming Tutorial
Memory Management

How to implement linked list with 1 million nodes?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

A linked list with one million nodes is not unusual from a data-structure perspective. The real question is whether a linked list is the right structure for that scale, because the implementation is easy but the memory and performance tradeoffs are often worse than people expect. If you still need one, build it iteratively, measure memory use, and avoid recursive operations.

A Million Nodes Is Feasible, but Not Free

A singly linked list node stores at least:

  • the payload
  • a reference or pointer to the next node
  • object overhead in managed runtimes such as Java or C#

That means one million nodes can consume far more memory than one million primitive values in an array. In Java, for example, a linked list of boxed integers has dramatically more overhead than an int[].

So before implementing the list, check whether you actually need:

  • frequent insertions in the middle with known node references
  • cheap splicing of nodes
  • stable node identity

If the real requirement is just storing one million items and iterating them, an array or ArrayList is usually better.

A Simple Java Implementation

Here is a minimal singly linked list that can hold one million integer nodes:

java
1public class MillionNodeList {
2    static class Node {
3        int value;
4        Node next;
5
6        Node(int value) {
7            this.value = value;
8        }
9    }
10
11    private Node head;
12    private Node tail;
13    private int size;
14
15    public void add(int value) {
16        Node node = new Node(value);
17        if (head == null) {
18            head = node;
19            tail = node;
20        } else {
21            tail.next = node;
22            tail = node;
23        }
24        size++;
25    }
26
27    public int size() {
28        return size;
29    }
30
31    public long sum() {
32        long total = 0;
33        Node current = head;
34        while (current != null) {
35            total += current.value;
36            current = current.next;
37        }
38        return total;
39    }
40}

The important part is keeping a tail reference. Without it, appending each new node would require walking the entire list, which would make construction O(n^2).

Building One Million Nodes Efficiently

Construction should be iterative:

java
1public static void main(String[] args) {
2    MillionNodeList list = new MillionNodeList();
3
4    for (int i = 0; i < 1_000_000; i++) {
5        list.add(i);
6    }
7
8    System.out.println("size = " + list.size());
9    System.out.println("sum = " + list.sum());
10}

This is O(n) time and uses one node allocation per element. For a million nodes, that is usually fine on a modern machine, but it still creates heavy allocation pressure compared with a contiguous array.

Avoid Recursive Traversal

A major mistake with large linked lists is recursive traversal:

java
1int count(Node node) {
2    if (node == null) {
3        return 0;
4    }
5    return 1 + count(node.next);
6}

That may look elegant, but on a list with one million nodes it will almost certainly trigger a stack overflow. Use loops for traversal, search, and deletion.

Iterative traversal is the correct approach:

java
1int countIterative(Node head) {
2    int count = 0;
3    Node current = head;
4    while (current != null) {
5        count++;
6        current = current.next;
7    }
8    return count;
9}

Think About Cache Behavior

Linked lists are often taught as efficient because insertion after a known node is cheap. That is true in a narrow algorithmic sense. But modern CPUs strongly favor contiguous memory. Arrays and vectors benefit from cache locality, while linked lists force the processor to chase pointers across memory.

For one million elements, this matters. Sequential array traversal is often much faster than linked-list traversal even though both are O(n) on paper.

This is why the answer is often "You can, but you probably should not unless the workload genuinely benefits from node linking."

Memory Use in Managed Languages

In Java, each node is an object. Object headers, alignment, and references all add overhead. If the payload is only an int, the bookkeeping may consume more memory than the actual data.

If memory efficiency matters, alternatives include:

  • primitive arrays such as int[]
  • array-backed lists
  • custom off-heap or packed structures for specialized systems

A million nodes is not extreme for heap size alone, but it is large enough that poor structural choices become visible quickly.

In C or C++, the Tradeoff Is Different

In C or C++, you can reduce overhead because the node layout is tighter and you control allocation directly:

c
1typedef struct Node {
2    int value;
3    struct Node* next;
4} Node;

That can be more memory-efficient than a managed object-per-node structure, but allocation strategy still matters. Calling malloc one million times works, but it may fragment memory and be slower than using an arena allocator or a pooled strategy.

What Operations Matter Most

Before committing to the linked list, define the workload:

  • append-heavy only
  • random lookup
  • middle insertion with node references
  • frequent deletion

If random access matters, linked lists are the wrong tool because accessing position k requires walking through k nodes. With one million nodes, that becomes expensive very quickly.

Common Pitfalls

  • Appending without a tail pointer and accidentally turning construction into O(n^2).
  • Using recursion for traversal or deletion and overflowing the call stack.
  • Assuming a linked list is memory-efficient for primitive data in managed languages.
  • Ignoring poor cache locality compared with arrays or array-backed lists.
  • Choosing a linked list when the real need is fast indexed access rather than pointer-based insertion.

Summary

  • A linked list with one million nodes is feasible, but it is often not the best data structure.
  • Build it iteratively and keep a tail pointer for efficient appends.
  • Avoid recursion on very large lists.
  • Measure memory and traversal costs, especially in managed runtimes.
  • Use an array or array-backed container instead when the workload does not specifically benefit from linked nodes.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.