00:00

Linked List Advantages and Disadvantages

Last updated:

What is Linked List?

A linked list stores its elements in separate nodes. Each node holds a value and a reference to the next node, and the program keeps a reference called the head to the first node. The last node's next field is null; an empty list has a null head.

Singly linked list: head points to the first node, each node points to the next, and the last node points to null

This design trades fast access by position for cheap changes to the shape of the list. The sections below show what you gain and what you pay for that trade.

Terminologies for Linked Lists

  • Head: the reference to the first node; every traversal starts here.

  • Node: one element of the list, with a data field and a link field.

  • Data: the value stored in the node.

  • Next Pointer: the reference to the following node, or null in the last node.

Advantages of Linked Lists

  • No fixed capacity: nodes are created one by one as values are added, so you never have to guess a size in advance or copy everything into a bigger array.

  • Fast insertion and deletion at a known place: adding or removing a node at the head, or right after a node you already hold, only changes one or two references, which is O(1). No other elements move.

  • No unused slots: the list holds exactly as many nodes as there are values. (Each node still carries the cost of its reference; see the disadvantages.)

  • Good base for other structures: stacks, queues, deques, adjacency lists and hash-table buckets are easy to build on a linked list.

  • No need for one large free block: because nodes can live anywhere in memory, a long list does not require a single contiguous region the way a big array does.

Disadvantages of Linked Lists

The same links that make changes cheap also make some things expensive:

  • No random access: to reach the k-th element you must follow k links from the head, which is O(k). Binary search loses its advantage for the same reason.

  • Extra memory per element: every node stores at least one reference (two in a doubly linked list) plus an object header in Java, so a list of small values uses much more memory than an array.

  • Poor cache behaviour: nodes are scattered in memory, so walking a list causes more cache misses than looping over an array, even though both are O(n).

  • One-way travel in a singly linked list: you cannot step back to the previous node; operations such as deleting the last node need a full walk to find its predecessor.

  • Memory management depends on references: in C or C++ a removed node must be freed by hand or it leaks. In Java the garbage collector reclaims a node once nothing refers to it, but a node that is still referenced (for example, by a forgotten tail or iterator variable) cannot be reclaimed.