datarekha

Linked Lists

Nodes chained by pointers, not laid out in contiguous memory: O(1) insert and delete at a known node, but O(n) access — the mirror image of an array's trade-off.

6 min read Intermediate GATE DA Lesson 57 of 122

What you'll learn

  • A linked list is nodes, each holding a value and a pointer to the next
  • Singly vs doubly linked: one forward pointer, or forward and backward
  • Insert/delete at a known node is O(1) — just relink the pointers
  • Access and search are O(n) — there is no index, you must traverse
  • Array vs linked list: opposite trade-offs, chosen by the dominant operation

Before you start

An array stores its elements in one contiguous block of memory, so element k lives at a known offset — instant access, but inserting in the middle means shifting everything after it. A linked list makes the opposite bargain: it scatters elements anywhere in memory and chains them with pointers (a pointer is just a stored reference to where the next node lives). Splicing a node in or out is trivial, but reaching the k-th element means walking the chain from the start. This same trade-off decides everyday design choices — an LRU cache, for instance, keeps its entries in a linked list precisely so it can move a “just-used” item to the front in O(1).

Nodes and pointers

A linked list is a sequence of nodes. Each node holds a value and a pointer to the next node; the last node points to nothing (null). A separate head reference marks the start. In a singly linked list each node points only forward; in a doubly linked list each node also stores a prev pointer, so you can walk backward and delete a node knowing only that node.

headABCnullEach box is a node: a value on the left, a next-pointer on the right.
No contiguous memory — the chain of next-pointers is what holds the order.

The cost trade-off

Because there is no index, access and search are O(n) — to reach the k-th node you must follow k pointers from the head. But once you are at a node, insert and delete are O(1): you just relink a couple of pointers, with no shifting of other elements. That is the exact reverse of an array, which indexes in O(1) but pays O(n) to insert or delete in the middle (everything after must shift).

operationarraylinked listaccess k-th / indexO(1)O(n)search by valueO(n)O(n)insert / delete at known nodeO(n)O(1)
Mirror-image trade-offs: arrays win on access, linked lists win on splicing at a known node.

Pick the operation you do most. Drive each one and watch what the two structures actually have to do:

One caveat the diagram hides: the O(1) insert/delete assumes you already hold the relevant node. If you must first find the position by value or index, that search is O(n) — the splice itself is still O(1), but getting there is not free.

How GATE asks this

Usually an MCQ or MSQ: it states an operation (traverse, insert after a node, delete a node) and asks for the cost, or it lists array-vs-linked-list claims and asks which are true. The reliable approach is to ask two questions — do I already have the node, or must I search for it? and am I accessing by position or relinking? — then read the cost straight off the table above.

Worked example — relinking is O(1), finding is O(n)

Start with the singly linked list A -> B -> C and a head pointing at A.

Insert a node X after the head. Two pointer writes: point X.next at whatever the head pointed to (B), then point head.next at X. No other node moves.

before:  head -> A -> B -> C
step 1:  X.next = head.next        (X now points to B)
step 2:  head.next = X             (A now points to X)
after:   head -> A -> X -> B -> C   (2 pointer updates, O(1))

Delete node B. Given the node before it (here A), one pointer write: re-point A.next to B.next (which is C). B is now unreachable and the chain is intact.

before:  A -> B -> C
step 1:  A.next = B.next           (A now points to C)
after:   A -> C                    (1 pointer update, O(1))

Both edits are O(1) once you hold the right node. Contrast finding the 4th element: with no index you must follow pointers head -> 1st -> 2nd -> 3rd -> 4th, an O(n) traversal. The relink is cheap; the navigation is what costs.

Quick check

Quick check

0/6
Q1In the singly linked list A -> B -> C, you delete node B given a pointer to its predecessor A. How many pointer (next) fields must be updated? (NAT)numerical answer — type a number
Q2Inserting a new node X immediately after the head of A -> B -> C. How many pointer (next) fields must be set? (NAT)numerical answer — type a number
Q3To access the 4th element of a singly linked list (1-indexed) starting from the head, how many next-pointers must you follow? (NAT)numerical answer — type a number
Q4Which statements correctly describe LINKED-LIST vs ARRAY trade-offs? (select all that apply)select all that apply
Q5Which statements about SINGLY vs DOUBLY linked lists are true? (select all that apply)select all that apply
Q6You need a structure where you frequently access elements by position (e.g. 'give me element 500') but rarely insert or delete. Which is the better fit?

Practice this in an interview

All questions

Sign in to track your progress

Completed lessons, your XP, level, and streak save to your account — it's free and takes a few seconds.

Explore further

Related lessons

Skip to content