Think about which data structure allows easy insertion and deletion without shifting elements.
Linked lists allow fast insertion and deletion anywhere because they only change pointers, unlike arrays which require shifting elements.
class Node { value: number; next: Node | null = null; constructor(value: number) { this.value = value; } } class LinkedList { head: Node | null = null; insertAtStart(value: number) { const newNode = new Node(value); newNode.next = this.head; this.head = newNode; } } const array: number[] = []; const linkedList = new LinkedList(); // Insert 3 elements at start array.unshift(1); array.unshift(2); array.unshift(3); linkedList.insertAtStart(1); linkedList.insertAtStart(2); linkedList.insertAtStart(3); console.log('Array:', array); let current = linkedList.head; const linkedListValues: number[] = []; while (current) { linkedListValues.push(current.value); current = current.next; } console.log('LinkedList:', linkedListValues);
Remember that unshift adds elements to the start of the array, and insertAtStart adds nodes at the head of the linked list.
Both array.unshift and linkedList.insertAtStart add elements at the beginning, so the newest element appears first in both.
class Node { value: number; next: Node | null = null; constructor(value: number) { this.value = value; } } const head: Node | null = null; let current = head; while (current.next !== null) { console.log(current.value); current = current.next; }
Check what happens when current is null and you try to access current.next.
Since head is null, current is null. Accessing current.next causes a TypeError because null has no properties.
Think about which structure avoids shifting elements when inserting or deleting in the middle.
Linked lists allow insertion and deletion anywhere by changing pointers, avoiding the costly shifting needed in arrays.
class Node { value: number; next: Node | null = null; constructor(value: number) { this.value = value; } } class LinkedList { head: Node | null = null; insertAtEnd(value: number) { const newNode = new Node(value); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } deleteAtStart() { if (this.head) { this.head = this.head.next; } } } const array: number[] = []; const linkedList = new LinkedList(); // Insert 1, 2, 3 at end array.push(1); array.push(2); array.push(3); linkedList.insertAtEnd(1); linkedList.insertAtEnd(2); linkedList.insertAtEnd(3); // Delete first element array.shift(); linkedList.deleteAtStart(); // Insert 4 at end array.push(4); linkedList.insertAtEnd(4); console.log('Array:', array); let current = linkedList.head; const linkedListValues: number[] = []; while (current) { linkedListValues.push(current.value); current = current.next; } console.log('LinkedList:', linkedListValues);
Remember that shift removes the first element from the array and deleteAtStart removes the head node from the linked list.
After removing the first element and adding 4 at the end, both array and linked list contain [2, 3, 4].
