Linked List vs Array When to Choose Which in DSA Javascript - Complexity Comparison
Choosing between a linked list and an array depends on how fast operations run as data grows.
We want to know which data structure handles adding, removing, and accessing items better as the list gets bigger.
Analyze the time complexity of basic operations on arrays and linked lists.
// Array operations
const arr = [];
arr.push(10); // Add at end
arr.unshift(5); // Add at start
const val = arr[1]; // Access by index
// Linked List operations
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
// Assume linked list with head pointer
// Adding at start: O(1)
// Access by index: O(n)
This code shows adding and accessing elements in arrays and linked lists.
Look at what repeats when we do operations on these structures.
- Primary operation: Accessing elements by index in array vs traversing nodes in linked list.
- How many times: Array access is direct (once), linked list access requires walking through nodes up to n times.
As the list size grows, accessing an element in an array stays fast, but in a linked list it gets slower.
| Input Size (n) | Array Access Steps | Linked List Access Steps |
|---|---|---|
| 10 | 1 | Up to 10 |
| 100 | 1 | Up to 100 |
| 1000 | 1 | Up to 1000 |
Pattern observation: Array access time stays the same no matter how big, linked list access time grows linearly with size.
Time Complexity: O(1) for array access, O(n) for linked list access
This means arrays let you get any item instantly, but linked lists need to check each item one by one.
[X] Wrong: "Linked lists are always faster because they use pointers."
[OK] Correct: Linked lists are faster only for adding/removing at start or middle without shifting, but accessing items by position is slower than arrays.
Understanding these differences helps you pick the right data structure for the job and explain your choice clearly in interviews.
"What if we used a doubly linked list instead of a singly linked list? How would the time complexity for adding or removing elements change?"
Practice
Solution
Step 1: Understand insertion and deletion costs in arrays
Arrays require shifting elements when inserting or deleting in the middle, which is costly.Step 2: Understand linked list insertion and deletion
Linked lists can insert or delete nodes by changing pointers without shifting elements.Final Answer:
Linked List -> Option DQuick Check:
Insert/delete middle = Linked List [OK]
- Thinking arrays handle middle insertions efficiently
- Confusing array resizing with insertion cost
- Assuming linked lists have faster access
Solution
Step 1: Understand linked list node structure
A linked list node typically has a value and a pointer to the next node, represented as an object with 'value' and 'next' properties.Step 2: Check each option
const node = { value: null, next: null }; correctly creates an object with 'value' and 'next' set to null. Others do not represent a linked list node properly.Final Answer:
const node = { value: null, next: null }; -> Option AQuick Check:
Linked list node = object with value and next [OK]
- Using arrays instead of objects for nodes
- Assigning null directly without properties
- Confusing array declaration with node
const arr = [1, 2, 3]; arr.splice(1, 1, 4); console.log(arr);
Solution
Step 1: Understand splice parameters
arr.splice(1, 1, 4) means start at index 1, remove 1 element, then insert 4 at that position.Step 2: Apply splice to array
Original array is [1, 2, 3]. Removing element at index 1 (which is 2) and inserting 4 results in [1, 4, 3].Final Answer:
[1, 4, 3] -> Option AQuick Check:
splice replaces element at index 1 with 4 [OK]
- Thinking splice inserts without deleting
- Confusing splice with slice
- Expecting original array unchanged
function insertAtHead(head, value) {
const newNode = { value: value, next: head };
head = newNode;
return head;
}
let head = null;
insertAtHead(head, 5);
console.log(head);Solution
Step 1: Understand variable scope in JavaScript
Inside the function, head is a local parameter. Assigning head = newNode changes only local copy.Step 2: Check effect on external head variable
Outside the function, head remains null because the updated head inside the function is not reflected outside.Final Answer:
The head variable is not updated outside the function -> Option BQuick Check:
Function parameter reassignment doesn't affect external variable [OK]
- Assuming head changes globally without return
- Not returning the new head
- Misunderstanding pointer assignment
Solution
Step 1: Analyze access by index requirement
Arrays provide constant time access by index, linked lists require traversal which is slower.Step 2: Analyze frequent append requirement
Arrays can append efficiently if capacity allows; linked lists also append efficiently but slower access is a drawback.Final Answer:
Array, because it allows fast access by index and efficient append -> Option CQuick Check:
Fast index access + append = Array [OK]
- Choosing linked list for fast index access
- Assuming linked list uses less memory always
- Confusing insertion in middle with append
Ready to prove it?
Pass the test and this pattern moves unknown
