What if you could add items instantly without moving everything else?
Why Push Using Linked List Node in DSA Typescript?
Imagine you have a stack of books on your desk. You want to add a new book on top, but you have to move all the books one by one to make space. This is like manually managing data without a proper structure.
Manually shifting items every time you add something is slow and confusing. You might lose track or make mistakes, especially if the stack is big. It's hard to keep everything organized and fast.
Using a linked list node to push means you just create a new node and link it on top. No shifting needed. It's like placing the new book directly on top without moving others. This keeps things simple, fast, and error-free.
let stack = [1, 2, 3]; // To push 4 manually: stack = [4].concat(stack);
class Node {
value: number;
next: Node | null;
constructor(value: number) {
this.value = value;
this.next = null;
}
}
let top: Node | null = null;
function push(value: number) {
const newNode = new Node(value);
newNode.next = top;
top = newNode;
}This lets you add items instantly on top, making stacks and other data structures fast and easy to manage.
Think of a browser's back button history. Each new page you visit is pushed on top, so you can go back step-by-step without rearranging the whole history.
Manual adding is slow and error-prone.
Push with linked list node adds instantly on top.
It keeps data organized and operations fast.
Practice
push operation do in a linked list when using nodes?Solution
Step 1: Understand push operation in linked list
The push operation adds a new node to the start (head) of the linked list.Step 2: Effect of push on linked list
The new node becomes the new head, pointing to the previous first node.Final Answer:
Adds a new node at the beginning of the list -> Option AQuick Check:
Push means add at head = D [OK]
- Confusing push with pop (removal)
- Thinking push adds at the end
- Mixing push with search or delete
Solution
Step 1: Create a new node with given value
Usenew Node(value)to create a node instance.Step 2: Link new node to current head and update head
SetnewNode.next = headand thenhead = newNodeto push at front.Final Answer:
const newNode = new Node(value); newNode.next = head; head = newNode; -> Option CQuick Check:
New node points to old head, head updated = A [OK]
- Forgetting to link new node's next to old head
- Assigning head.next instead of newNode.next
- Not updating head to new node
class Node {
value: number;
next: Node | null = null;
constructor(value: number) { this.value = value; }
}
let head: Node | null = null;
function push(value: number) {
const newNode = new Node(value);
newNode.next = head;
head = newNode;
}
push(10);
push(20);
push(30);Solution
Step 1: Push 10 to empty list
Head becomes node with value 10, next is null.Step 2: Push 20 and then 30
Each new node points to previous head, so order is 30 -> 20 -> 10 -> null.Final Answer:
30 -> 20 -> 10 -> null -> Option DQuick Check:
Last pushed is head = A [OK]
- Assuming push adds at tail
- Reading list in wrong order
- Confusing next pointers
function push(value: number) {
const newNode = new Node(value);
head.next = newNode;
head = newNode;
}Solution
Step 1: Check head usage before assignment
The code useshead.nextwithout checking if head is null.Step 2: Consequence of head being null
If list is empty (head null), accessinghead.nextcauses runtime error.Final Answer:
head might be null causing runtime error -> Option AQuick Check:
Accessing head.next without null check = B [OK]
- Not checking for empty list before push
- Forgetting to set newNode.next
- Assigning head incorrectly
Also, explain how the head changes after each push.
Solution
Step 1: Initial list and push 35
Original head is 5. Push 35 creates new node 35 pointing to 5. Head updates to 35.Step 2: Push 45 and update head
Push 45 creates new node 45 pointing to 35. Head updates to 45. Final list: 45 -> 35 -> 5 -> 15 -> 25 -> null.Final Answer:
45 -> 35 -> 5 -> 15 -> 25 -> null; head moves to newest node each push -> Option BQuick Check:
Push adds at front, head updates = C [OK]
- Assuming push adds at tail
- Not updating head after push
- Confusing node order after pushes
Ready to prove it?
Pass the test and this pattern moves unknown
