Linked List Data Structure: Complete Guide with Pointer Traces and Worked Examples
Learn how linked lists work by tracing every pointer change in core operations, then apply the same invariants to reversal, middle-node search, and cycle detection.
KnowledgeGate Team
Exam prep & CS education

A linked list looks simple when it is drawn as boxes and arrows. Questions become difficult when head, next, prev, and temporary pointers start changing during an operation. Insertion, deletion, reversal, middle-node search, and cycle detection can all be traced through pointer changes. The same pointer discipline helps with exam-style tracing and coding interviews under time pressure.
Linked list data structure: nodes, links and the array contrast
A node is a record containing data and one or more links. In head -> N1(10) -> N2(25) -> N3(40) -> null, head refers to the first node, while null marks the end. An optional tail can refer directly to N3.
Compare that with the array [10, 25, 40]. Array elements occupy indexed logical positions, so accessing the third element is direct. Linked-list nodes may sit separately in memory and preserve their order through links. Reaching N3 requires visiting N1, then N2, then N3.
This structure supports flexible growth and local pointer changes, but each link needs storage. It also has weaker cache locality and sequential access. An empty list has head = null. In a one-node list, head = tail = N1 and N1.next = null.
Singly linked list operations: insertion and deletion worked completely
To search 10 -> 25 -> 40 -> null, initialise current = head. Compare 10, advance with current = current.next, compare 25, advance again, and compare 40. A search for 40 visits three nodes. An unsuccessful search stops when current == null.
Now start with these links:
N1(10).next = N2N2(25).next = N3N3(40).next = null
Insert N4(18) after N1 in a safe order. First set N4.next = N1.next, so N4 points to N2. Then set N1.next = N4. The result is 10 -> 18 -> 25 -> 40 -> null. Reversing those assignments would overwrite the only link from N1 to N2, unless that reference had already been saved.
To delete 25, let prev = N4 and current = N2. Assign prev.next = current.next. This changes N4.next from N2 to N3. Release or discard N2 as the language requires. The final list is 10 -> 18 -> 40 -> null. Deleting by value includes a search. Deleting a node when its predecessor is already known only needs the relinking step.

Singly, doubly and circular linked lists
Choose links for movement.
Variant | Node fields | End condition | Useful operation | Cost |
|---|---|---|---|---|
Singly |
|
| Forward scan | One link |
Doubly |
| Boundary | Backward scan, known-node deletion | Two links |
Circular singly |
| Back at | Repeated traversal | No null end |
For D1(7) <-> D2(14) <-> D3(21), delete known D2: set D1.next = D3, set D3.prev = D1, then detach D2. Check head and tail boundaries.
In C1(3) -> C2(6) -> C3(9) -> C1, a null condition fails because C3.next = C1. Stop on return to head. A sentinel can remove branches, but is an implementation technique, not another list type.
Reverse a linked list without losing nodes
Reverse the post-deletion list 10 -> 18 -> 40 -> null. Before each iteration, prev heads the reversed prefix, current heads the unreversed suffix, and next temporarily preserves that suffix before current.next is overwritten.
State |
|
| Saved | Rewritten link |
|---|---|---|---|---|
Initial |
|
| None | |
Iteration 1 |
|
|
|
|
Iteration 2 |
|
|
|
|
Iteration 3 |
|
|
|
|
Iteration 1 saves next = 18, writes 10.next = null, then moves prev = 10 and current = 18. Iteration 2 saves 40, writes 18.next = 10, and moves both pointers. Iteration 3 saves null, writes 40.next = 18, and leaves current = null. Finally assign head = prev. The result is 40 -> 18 -> 10 -> null.
Each node changes its next link once. No node is lost because the suffix is saved first. The traversal takes O(n) time and the iterative method uses O(1) auxiliary space. Recursive reversal also takes O(n) time, but consumes call-stack space.

Two-pointer patterns: middle node and cycle detection
For 5 -> 10 -> 15 -> 20 -> 25 -> null, start both pointers at 5. Step 1 gives slow = 10, fast = 15. Step 2 gives slow = 15, fast = 25. Fast cannot take two more links, so 15 is the middle. The same initialisation on 5 -> 10 -> 15 -> 20 returns the second middle, 15.
For Floyd's cycle detection on A(10) -> B(18) -> C(40) -> D(55) -> B, start both at A. The positions are step 1 (B, C), step 2 (C, B), and step 3 (D, D). Meeting at D proves a cycle. Compare pointer identity, not data values, because different nodes may hold equal values.
Related patterns merge sorted lists by relinking nodes, find the kth node from the end with a fixed gap, and locate a cycle entry by resetting one pointer to head after the first meeting.
Linked list complexity and traps
Every O(1) claim needs its precondition.
Operation | Cost and condition |
|---|---|
Traverse or value search |
|
Indexed access |
|
Head insertion or deletion |
|
Insert after known node |
|
Tail insertion |
|
Singly linked deletion |
|
Known internal doubly linked node deletion |
|
Overwriting next loses the suffix, so save it first. Missing a head update leaves a stale entry, so handle that boundary. A null-based circular loop never ends, so stop at head. Equal data can falsely signal a cycle, so compare references.
Never use a detached node. Manual-memory languages must free it once. Garbage-collected languages need unintended references removed. Lists do not always beat arrays; cost depends on operations and references.
Linked list questions in GATE and interviews
GATE questions can ask you to trace a short pointer fragment, choose a complexity with its preconditions, identify the result of insertion or deletion, or reason about slow and fast pointers. These tasks test whether you can preserve references while the structure changes.
Interview tasks combine implementation with explanation. You may need to reverse a list, find its middle, detect a loop, merge sorted lists, and cover empty and one-node inputs. Use Data Structures MCQs for an immediate concept check after learning the mechanics.
Trees are a sensible next concept because they also use nodes and links, but allow branching instead of one next-chain. Binary Trees and Binary Search Trees develops that change in structure and traversal.
Linked list data structure: the short version and next step
Nodes carry data plus links.
Traversal is sequential.
Local updates are constant-time only when the required node references are already known.
Save
nextbefore rewiring.Test empty, one-node, head, tail, and circular boundaries.
At your desk, redraw 10 -> 18 -> 40, reverse it to 40 -> 18 -> 10, then restore it without looking at the trace. For structured subject coverage, use GATE Guidance by Sanchit Sir. For language-based implementation and interview practice, use DSA Using Python as the alternative.
Keep learning

Queue Data Structure: Operations, Circular Queues and Worked Examples for GATE
Learn queue operations through a five-slot circular queue trace, then connect the same FIFO rule to linked lists, BFS and the two-stack implementation.

Data Structures: Types, Operations, Complexity, and Worked Examples
Learn how representations and operation costs shape data-structure choices. Follow one array and linked-list edit sequence, then apply the reasoning to stacks, queues, trees, and hashing.

Hashing Data Structure: Hash Functions, Collision Resolution and Worked Examples
Trace the same eight keys through separate chaining and linear probing, then learn how tombstones, load factor and rehashing affect correctness and speed.

Data Structure for GATE: Syllabus Map, Past-Paper Weightage and Preparation Order
Map the official GATE Data Structure scope, read the two 2026 CS sessions without turning them into a forecast, and follow a verified 48-hour study order.