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

Updated 29 Sep 20266 min read

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.

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 = N2

  • N2(25).next = N3

  • N3(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.

Three panels: the list 10, 25, 40; node 18 inserted after 10; then 25 deleted so 18 links to 40.

Singly, doubly and circular linked lists

Choose links for movement.

Variant

Node fields

End condition

Useful operation

Cost

Singly

data, next

null

Forward scan

One link

Doubly

prev, data, next

Boundary null

Backward scan, known-node deletion

Two links

Circular singly

data, next

Back at head

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

prev

current

Saved next

Rewritten link

Initial

null

10

None

Iteration 1

null

10

18

10 -> null

Iteration 2

10

18

40

18 -> 10

Iteration 3

18

40

null

40 -> 18

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.

Reversal trace for 10, 18, 40 listing prev, current, saved next and the link rewritten each iteration.

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

O(n)

Indexed access

O(n) from head

Head insertion or deletion

O(1)

Insert after known node

O(1)

Tail insertion

O(1) with tail; otherwise O(n)

Singly linked deletion

O(1) with predecessor

Known internal doubly linked node deletion

O(1) through prev and next

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 next before 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.