linked list

 

📊 Big-O Comparison: Array vs Linked List

🔹 Time Complexity

OperationArrayLinked ListNotes
Access by indexO(1)O(n)Array has direct indexing
Search (unsorted)O(n)O(n)Same for both
Search (sorted)O(log n)O(n)Binary search only works on arrays
Insert at beginningO(n)O(1)Array needs shifting
Insert at endO(1)*O(n) / O(1)O(1) if tail pointer exists
Insert at middleO(n)O(n)Traversal needed in LL
Delete at beginningO(n)O(1)Shifting in array
Delete at endO(1)O(n)Need previous node
Delete at middleO(n)O(n)Traversal needed
TraversalO(n)O(n)Same
Update by indexO(1)O(n)No index in LL
ReverseO(n)O(n)Logic differs
Find predecessorO(1)O(n)Easy in arrays

* Amortized O(1) for dynamic arrays


🔹 Space Complexity

StructureSpace
ArrayO(n)
Linked ListO(n) + pointers

Linked list uses extra memory for pointers (next, prev).


🔹 Key Differences (Quick View)

FeatureArrayLinked List
MemoryContiguousNon-contiguous
IndexingYesNo
Cache friendlyYesNo
Dynamic size❌ (static) / ✅ (dynamic array)
Pointer overhead

🔹 When to Use What?

✅ Use Array when:

  • Fast access by index is needed

  • Memory efficiency matters

  • Mostly read operations

✅ Use Linked List when:

  • Frequent insertions/deletions

  • Size changes often

  • No need for random access


🧠 Interview One-Liner

Arrays offer O(1) access but costly insertions, while linked lists allow fast insertions but slow access.


Comments

Popular posts from this blog

interview questions js[ Anurag Singh ProCodrr]

reactnative_creation