00:00

Linked List Data Structures

Last updated:

What is Linked List?

A linked list is a linear data structure built from small objects called nodes. Each node stores a value and a reference to the next node, so the nodes form a chain. Unlike an array, the list has no index arithmetic: to reach an element you start at the front and follow the references.

The program keeps one reference, the head, which points to the first node. If the list is empty, the head is null. The last node, often called the tail, has null in its next field, which tells a loop where the list ends.

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

Nodes are created one at a time, at run time, only when a value is added. The list therefore never runs out of fixed capacity, and adding or removing a node only changes a few references instead of shifting other elements. The price is extra memory for the references and slower access by position.

Because nodes can live anywhere in memory, a linked list is a natural building block for other structures: stacks, queues, adjacency lists for graphs, and the buckets of a hash table that uses separate chaining.

Terminologies for Linked Lists

  • Head: the reference to the first node, and the starting point of every traversal.

  • Node: one element of the list, holding 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.

Types of Linked Lists in Data Structures

  1. Singly Linked List: each node links only to the next node, so the list can be walked in one direction.

  2. Doubly Linked List: each node links to both the next and the previous node, so the list can be walked in both directions.

  3. Circular Linked List: the last node links back to the first node instead of to null.

Complexity Analysis of Linked List Operations

  • Time Complexity (singly linked list with a head reference, n nodes)
    OperationsBest CaseAverage CaseWorst case
    TraversalO(n)O(n)O(n)
    InsertionO(1) (at the head)O(n) (the position must be reached first)O(n)
    DeletionO(1) (at the head)O(n) (the node and its predecessor must be found first)O(n)
    SearchO(1) (value is in the first node)O(n)O(n)
  • Space Complexity: the list itself needs O(n) memory for n nodes. Insertion, deletion, search and iterative traversal need only O(1) extra space.