Docsity

Array vs Singly, Doubly, Circular Linked List Notes – DSA Time Complexity Comparison Guide, Study notes of Computer Science

Toggle sidebar
bg1
DATA STRUCTURES
Array vs Singly vs Doubly vs Circular Linked List
Complete Comparison Guide with Time Complexity Charts
What You'll Master in These Notes
•A side-by-side recap of all four structures — with diagrams for each
•Access, Search, Insertion, and Deletion time complexity — compared head-to-head
•One master comparison table covering all four structures and all major operations
•Memory overhead comparison — what each structure actually costs you
•A practical 'When to Use Which' decision guide
•5 scenario-based solved examples that mirror real exam and interview questions
This is a fact-checked, 15-page reference guide — every time complexity entry is explained with the
reasoning behind it, not just stated as a bare label. Ideal as a single revision sheet that ties together
everything taught across separate Array and Linked List chapters.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Array vs Singly, Doubly, Circular Linked List Notes – DSA Time Complexity Comparison Guide and more Study notes Computer Science in PDF only on Docsity!

DATA STRUCTURES

Array vs Singly vs Doubly vs Circular Linked List

Complete Comparison Guide with Time Complexity Charts

What You'll Master in These Notes

  • A side-by-side recap of all four structures — with diagrams for each
  • Access, Search, Insertion, and Deletion time complexity — compared head-to-head
  • One master comparison table covering all four structures and all major operations
  • Memory overhead comparison — what each structure actually costs you
  • A practical 'When to Use Which' decision guide
  • 5 scenario-based solved examples that mirror real exam and interview questions

This is a fact-checked, 15-page reference guide — every time complexity entry is explained with the reasoning behind it, not just stated as a bare label. Ideal as a single revision sheet that ties together everything taught across separate Array and Linked List chapters.

The Question Every DSA Interview Eventually Asks

"Why not just use an array for everything?" is one of the most common follow-up questions in technical

interviews and viva exams — and it's also one of the most revealing. The honest answer is that no

single data structure wins on every measure. Each one trades something away to gain something else,

and knowing exactly what that trade is what separates a memorized answer from real understanding.

Why This Comparison Matters Beyond Exams

Every time an engineer picks a data structure for a real system, this exact comparison is happening in the background. A web browser choosing how to store tab history, a music app choosing how to manage a playlist queue, a game engine choosing how to cycle through players' turns — all of these decisions come down to the same four questions this document answers: How fast is access? How fast is search? How fast is insertion? How fast is deletion?

A Preview of the Core Trade-off

Arrays are unbeatable when you need to jump straight to the 500th element instantly — but painfully slow if you're constantly inserting at the front. Linked lists flip this completely: inserting at the front is instant, but reaching the 500th element means walking through all 499 before it. Doubly and Circular variants each solve one more specific pain point on top of this — and by the end of this document, you'll be able to justify, with reasoning (not guesswork), exactly which structure fits which situation.

Keep this core trade-off in mind as you go through the detailed breakdown ahead — every table and diagram in this guide is really just this one idea, examined operation by operation.

1. Quick Recap: The Four Structures at a Glance

1.1 Array

Elements stored in contiguous memory, accessed directly by index using Base Address + (index × element size).

1.2 Singly Linked List

10 20 30 NULL

Nodes scattered in memory, each storing data and a single next pointer; traversal is forward-only, ending at NULL.

1.3 Doubly Linked List

Each node stores data plus both a next and a prev pointer, enabling traversal in both directions.

1.4 Circular Linked List (Singly variant shown)

10

20

30

40

The last node's next points back to the first node instead of NULL — the list has no true end. (A Circular Doubly variant also exists, combining this wrap-around with bidirectional pointers.)

2. Access Time Comparison

"Access" means reaching the element at a given position (e.g., the 5th element) directly.

Structure Access Time Reason

Array O(1) Direct address calculation: Base + index × element size

Singly Linked List O(n) Must traverse from HEAD, one node at a time, to reach the position

Doubly Linked List O(n) Same as singly — no direct index formula exists for linked nodes

Circular Linked List (Singly/Doubly) O(n) Same traversal limitation as their linear counterparts

Key takeaway: Array is the clear winner for access time — this is its single biggest structural advantage, and it holds true across all linked list variants because none of them support a direct address formula.

3. Search Time Comparison

"Search" means finding whether a given value exists in the structure (using Linear Search, since none of these structures are inherently sorted).

Structure Search Time Reason

Array O(n) Unsorted linear search checks each element one by one

Singly Linked List O(n) Traverse node by node from HEAD until a match or NULL

Doubly Linked List O(n) Same as singly for forward search; backward search from TAIL is also O(n)

Circular Linked List O(n) Must traverse and explicitly detect the loop back to HEAD to stop

Key takeaway: Search time is O(n) across the board for unsorted data in every structure discussed here — none has a built-in advantage. (Sorted arrays are the one exception, where Binary Search brings this down to O(log n) — but that requires the data to be sorted first.)

5. Deletion Time Comparison (Beginning, End, Middle)

Structure At Beginning At End At Middle

Array O(n) O(1) O(n)

Singly Linked List O(1) O(n) O(n)

Doubly Linked List O(1) O(n)† O(n)

Circular Singly LL O(n)‡ O(n)‡ O(n)

Circular Doubly LL O(1)§ O(1)§ O(n)

† O(n) because reaching the second-last node requires traversal; becomes O(1) if TAIL is maintained (since TAIL->prev gives the second-last node directly). ‡ O(n) because the last node must be located in order to update its pointer, whether deleting the first or last element. § O(1) because both the last node and second-last node are instantly reachable via prev pointers.

Key takeaway: Linked lists dominate at the beginning (O(1) for Singly/Doubly), while arrays dominate at the end (O(1) for both insertion and deletion, assuming space is available). Middle operations remain O(n) for every structure discussed here, since locating the position always requires either shifting (array) or traversal (linked list).

6. Master Comparison Table — All Structures, All

Operations

Operation Array Singly LL Doubly LL Circular Singly Circular Doubly

Access by position O(1) O(n) O(n) O(n) O(n)

Search (unsorted) O(n) O(n) O(n) O(n) O(n)

Insert at Beginning O(n) O(1) O(1) O(n) O(1)

Insert at End O(1)* O(n) O(n) O(n) O(1)

Insert at Middle O(n) O(n) O(n) O(n) O(n)

Delete at Beginning O(n) O(1) O(1) O(n) O(1)

Delete at End O(1) O(n) O(n) O(n) O(1)

Delete at Middle O(n) O(n) O(n) O(n) O(n)

Traversal direction Either (indexed) Forward only Both Forward only (loops) Both (loops)

Extra NULL/loop handling None Ends at NULL Ends at NULL Must detect HEAD Must detect HEAD

  • Assuming space is already available; O(n) if resizing is required. Singly/Doubly figures for End operations assume no separate TAIL pointer is maintained (all become O(1) with one, except Array resizing, which is a separate memory-allocation cost).

9. Pros and Cons of Each Structure

9.1 Array

  • Pros:^ O(1)^ direct^ access^ by^ index;^ no^ per-element^ memory^ overhead;^ simple^ to^ implement; cache-friendly due to contiguous memory, which makes real-world iteration faster than its Big O alone suggests.
  • Cons:^ Fixed size in static arrays; O(n) insertion/deletion at the beginning or middle; resizing a dynamic array (when full) requires an O(n) copy to a new block.

9.2 Singly Linked List

  • Pros:^ O(1) insertion/deletion at the beginning; dynamic size — grows and shrinks one node at a time with no resizing/copying step; no wasted pre-allocated memory.
  • Cons:^ O(n) access and search; no backward traversal; extra memory per node for the next pointer; weaker cache performance than an array, since nodes are scattered in memory.

9.3 Doubly Linked List

  • Pros:^ All the dynamic-size benefits of a singly linked list, plus O(1) backward traversal and O(1) deletion when a node's address is already known (no need to search for its predecessor).
  • Cons:^ Double^ the^ pointer^ overhead^ per^ node^ compared^ to^ a^ singly^ linked^ list;^ every insertion/deletion must correctly update two pointers instead of one, which increases implementation complexity and the chance of pointer-handling bugs.

9.4 Circular Linked List (Singly and Doubly)

  • Pros:^ Naturally models cyclical problems (round-robin scheduling, turn-based systems) without extra wrap-around logic; from any node, the entire list is reachable, which is useful for structures meant to be entered from any point.
  • Cons:^ Traversal logic must explicitly detect the return to HEAD, or it will loop forever — a common source of bugs for beginners; the Circular Singly variant loses the O(1) beginning-insertion advantage that a linear Singly Linked List has, since the last node must still be located to update its pointer.

10. Solved Examples

Solved Example 1: Choosing a structure for frequent front insertions

Problem: A logging system needs to add 50,000 new log entries at the front of a list (most recent first) and rarely accesses entries by position. Which structure is best?

Reasoning:

  • Array insertion at the beginning is O(n) — with 50,000 entries, every single insertion would shift up to 50,000 elements.
  • Singly Linked List insertion at the beginning is O(1) — regardless of how many entries already exist. Final Answer: A Singly Linked List is best here — no random access is needed, and front insertion is the dominant operation.

Solved Example 2: Choosing between Singly and Doubly Linked List

Problem: An application needs to delete arbitrary nodes frequently, and at the time of deletion, a direct pointer to the node (not its position) is already available. Which structure minimizes the cost of deletion?

Reasoning:

  • In a Singly Linked List, deleting a node whose pointer is known still requires finding its predecessor by traversing from HEAD — O(n).
  • In a Doubly Linked List, the predecessor is available instantly via the node's own prev pointer — deletion becomes O(1) once the node's address is known.

Final Answer: A Doubly Linked List is better suited, since it avoids the O(n) predecessor search entirely.

Solved Example 3: Access-heavy workload

Problem: A program needs to repeatedly access the 750th element of a 1,000-element collection, with insertions/deletions happening rarely. Which structure fits best?

Reasoning:

  • Any linked list variant (Singly, Doubly, or Circular) would need O(n) traversal every time the 750th element is accessed.
  • An Array accesses the 750th element directly in O(1) using the address formula, regardless of how many times it is accessed.

Final Answer: An Array is clearly best here, since access is the dominant, frequent operation and insertions/deletions are rare.

Solved Example 4: Cyclical turn-based requirement

Problem: A game engine must cycle through 8 players' turns repeatedly, indefinitely, with no fixed 'last' player. Which structure naturally fits this requirement?

Reasoning:

  • An Array or linear Linked List has a definite end, requiring extra logic (like modulo arithmetic or manually resetting an index/pointer) to wrap back to the first player.
  • A Circular Linked List has no true end — continuing to call next() naturally cycles back to the first player without any extra wrap-around logic.

Final Answer: A Circular Linked List (Singly, since only forward turn order is needed) is the natural fit.

Solved Example 5: Combining requirements — Music player with Next and Previous

Solved Example 6: Undo/Redo feature design

Problem: A design tool needs an Undo/Redo history where the user can move backward through past actions and then forward again if they change their mind. Which structure fits, and why not a Singly Linked List?

Reasoning:

  • Undo moves one step backward; Redo moves one step forward — both directions are needed from wherever the user currently is in the history.
  • A Singly Linked List has no prev pointer, so moving backward from the current action would require re-traversing from HEAD every time — inefficient and impractical for frequent Undo presses.
  • A Doubly Linked List supports O(1) movement in both directions from the current node. Final Answer: A Doubly Linked List is the correct choice, since Undo/Redo is fundamentally a bidirectional-movement problem.

Solved Example 7: Memory-constrained embedded system

Problem: An embedded system with very limited memory needs to store a fixed-size list of sensor readings and mainly reads them by position (index). Insertions/deletions are rare. Which structure minimizes memory usage?

Reasoning:

  • Any linked list variant (Singly, Doubly, or Circular) adds at least one pointer of overhead per element, which is wasted memory here since insertions/deletions are rare.
  • An Array stores only the data itself, with zero pointer overhead, and also gives O(1) access for the frequent by-position reads this system needs.

Final Answer: An Array is the best fit — it is both the most memory-efficient option and the fastest for the dominant access-by-position operation.

Solved Example 8: Comparing worst-case deletion cost

Problem: For a list of 1,000 elements, compare the worst-case number of pointer/element operations needed to delete the last element in: (a) an Array with a maintained size counter, (b) a Singly Linked List with no TAIL pointer, and (c) a Doubly Linked List with no TAIL pointer.

Step-by-step reasoning:

  • (a) Array: deleting the last element only requires decrementing the size counter — a constant-time O(1) operation, regardless of the 1,000 elements.
  • (b) Singly Linked List: without a TAIL pointer, the list must be traversed from HEAD to find the second-last node — up to 999 pointer hops in the worst case, making this O(n).
  • (c) Doubly Linked List: without a TAIL pointer, the list must similarly be traversed from HEAD to find the last node — also up to 1,000 pointer hops, O(n). (Note: if a TAIL pointer were maintained, this would drop to O(1) for the Doubly Linked List, since TAIL->prev gives the second-last node directly — this is not possible for a Singly Linked List even with a TAIL pointer, since there is no prev pointer to look backward with.)

Final Answer: Array is O(1); Singly and Doubly Linked Lists are both O(n) without a TAIL pointer — but only the Doubly Linked List can be brought down to O(1) simply by adding a TAIL pointer, since a Singly Linked List still cannot look backward from TAIL.

11. Common Mistakes to Avoid

  • Assuming linked list insertion is always O(1):^ only insertion at the^ beginning^ (or at the end with a maintained TAIL pointer) is O(1) — insertion at the middle is always O(n) because the position must first be reached by traversal.
  • Writing 'while (temp != NULL)' for a circular linked list traversal:^ this causes an infinite loop, since no node's next pointer is ever NULL in a circular list — the loop must check against HEAD instead.
  • Forgetting the single-node edge case:^ when a linked list has exactly one node, deleting it requires special handling (setting HEAD to NULL, or in a circular list, making the node point to itself before deletion) — code that assumes at least two nodes will crash or behave incorrectly.
  • Assuming^ an^ array^ is^ always^ better^ because^ of^ O(1)^ access:^ this^ ignores^ the^ cost^ of insertion/deletion at the beginning or middle, which is exactly where linked lists outperform arrays — the right structure always depends on which operation dominates the actual workload.
  • Confusing 'Doubly Linked List' with 'sorted':^ having a prev pointer has nothing to do with whether the data is sorted — search remains O(n) in every structure covered in this document unless the data is additionally sorted and a different algorithm (like Binary Search on a sorted array) is used.

Quick Revision Box

Array: best for access (O(1)) and end operations; worst for beginning/middle insertion-deletion (O(n)). Singly LL: best for beginning insertion/deletion (O(1)); worst for access (O(n)). Doubly LL: adds O(1) deletion-when-node-known and backward traversal, at the cost of extra pointer memory. Circular Singly LL: useful for cyclical problems, but beginning operations cost O(n) unless a TAIL pointer is kept. Circular Doubly LL: combines cyclical wrap-around with O(1) operations at both ends, at the highest memory cost of the five.

— End of Notes —