Discover why your favorite apps choose different ways to store lists and how it makes your experience smooth or slow!
Linked List vs Array When to Choose Which in DSA Typescript - Why the Distinction Matters
Imagine you have a long list of your favorite songs written on paper. You want to add new songs, remove some, or find a song quickly. If you write them all in one long line (like an array), adding or removing songs in the middle means rewriting many songs. If you write each song on a separate card and link them with strings (like a linked list), you can add or remove cards easily without rewriting the whole list.
Using just a simple list (array) means every time you add or remove a song in the middle, you must shift many songs up or down. This takes time and can cause mistakes if you lose track. Also, if your list grows beyond the paper size, you need a bigger paper and rewrite everything. This is slow and frustrating.
Linked lists solve this by letting you connect each song card to the next with a string. You can add or remove cards by just changing the strings, without moving all cards. Arrays let you find songs quickly by position, but linked lists let you change the list easily without big rewrites.
let songs = ['Song1', 'Song2', 'Song3']; songs.splice(1, 0, 'NewSong'); // shifts elements after index 1
class Node {
value: string;
next: Node | null;
constructor(value: string) {
this.value = value;
this.next = null;
}
}
// Insert new node by changing links, no shifting neededYou can choose the best way to store and change your list depending on whether you need fast access or easy updates.
Music apps use arrays to quickly play songs by number, but use linked lists to manage playlists where songs are often added or removed.
Arrays are great for quick access by position but slow for inserting/removing in the middle.
Linked lists are great for easy insertion/removal but slower to find items by position.
Choosing between them depends on whether you prioritize speed of access or ease of updates.
Practice
Solution
Step 1: Understand random access in arrays
Arrays store elements in contiguous memory, so accessing by index is fast and direct.Step 2: Understand random access in linked lists
Linked lists require traversal from the head to reach an index, which is slower.Final Answer:
Array -> Option CQuick Check:
Fast random access = Array [OK]
- Thinking linked lists allow fast index access
- Confusing stack or queue with arrays
- Assuming all data structures have same access speed
Solution
Step 1: Understand TypeScript class instantiation
To create an instance, use the 'new' keyword with constructor parameters.Step 2: Check constructor parameters
LinkedListNode usually requires a value; null can represent empty data.Final Answer:
const node = new LinkedListNode(null); -> Option AQuick Check:
Use 'new' with constructor args for instances [OK]
- Omitting 'new' keyword
- Calling class like a function without 'new'
- Passing no arguments when constructor expects one
class Node {
value: number;
next: Node | null;
constructor(value: number) {
this.value = value;
this.next = null;
}
}
const head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
let current = head;
const result: number[] = [];
while (current !== null) {
result.push(current.value);
current = current.next;
}
console.log(result);Solution
Step 1: Build the linked list nodes
Nodes created with values 1, 2, 3 linked in order: 1 -> 2 -> 3 -> null.Step 2: Traverse and collect values
Loop pushes values 1, then 2, then 3 into result array.Final Answer:
[1, 2, 3] -> Option AQuick Check:
Traverse linked list in order = [1, 2, 3] [OK]
- Confusing order of nodes
- Not updating current pointer correctly
- Expecting reversed output
class Node {
value: number;
next: Node | null;
constructor(value: number) {
this.value = value;
this.next = null;
}
}
let head = new Node(1);
head.next = new Node(2);
function insertAtStart(newValue: number) {
const newNode = new Node(newValue);
newNode.next = head.next;
head = newNode;
}
insertAtStart(0);
let current = head;
const result: number[] = [];
while (current !== null) {
result.push(current.value);
current = current.next;
}
console.log(result);Solution
Step 1: Analyze insertion logic
New node's next is set to head.next, skipping the current head node.Step 2: Correct pointer assignment
To insert at start, newNode.next must point to current head, preserving the list.Final Answer:
newNode.next should point to head, not head.next -> Option BQuick Check:
Insert at start = newNode.next = head [OK]
- Setting newNode.next to head.next causes node loss
- Not updating head reference properly
- Confusing next pointers in linked list
Solution
Step 1: Consider insertion and deletion in arrays
Arrays require shifting elements when inserting or deleting in the middle, which is slow.Step 2: Consider linked list advantages
Linked lists can insert or delete nodes by changing pointers without shifting elements, making these operations faster.Final Answer:
Linked List, because it allows fast insertion and deletion without shifting elements. -> Option DQuick Check:
Frequent middle insert/delete = Linked List [OK]
- Choosing array for frequent middle insert/delete
- Thinking linked lists support binary search efficiently
- Ignoring cost of shifting elements in arrays
Ready to prove it?
Pass the test and this pattern moves unknown
