Bird
Raised Fist0
DSA Typescriptprogramming~15 mins

Push Using Linked List Node in DSA Typescript - Deep Dive

Choose your learning style10 modes available
Overview - Push Using Linked List Node
What is it?
Push using a linked list node means adding a new element at the start of a linked list. A linked list is a chain of nodes where each node holds data and a link to the next node. When we push, we create a new node and make it the new head of the list. This operation changes the list by inserting the new node before all others.
Why it matters
Without the ability to push nodes, linked lists would be hard to build and update efficiently. Push lets us add elements quickly at the front without moving other nodes. This makes linked lists useful for stacks, undo features, and many real-time systems where fast insertions matter. Without push, linked lists lose much of their flexibility and speed advantage.
Where it fits
Before learning push, you should understand what a linked list and a node are. After push, you can learn about other operations like pop, append, and traversal. Push is a foundational step to mastering linked list manipulation and understanding dynamic data structures.
Mental Model
Core Idea
Pushing a node means creating a new node and linking it before the current first node, making it the new head.
Think of it like...
Imagine a line of people holding hands. To add a new person at the front, you ask the new person to hold the hand of the first person, then you become the new first person in line.
Head -> [New Node] -> [Old First Node] -> [Next Node] -> null
Build-Up - 7 Steps
1
FoundationUnderstanding Linked List Nodes
🤔
Concept: Learn what a node is and how it stores data and a link to the next node.
A node is a small container with two parts: one for data (like a number or word) and one for a link to the next node. For example, a node holding number 5 points to the next node or null if none exists.
Result
You can picture a chain of nodes, each connected to the next, forming a linked list.
Knowing what a node is helps you understand how linked lists store and connect data step-by-step.
2
FoundationWhat is the Head of a Linked List?
🤔
Concept: The head is the first node in the linked list and entry point to the entire list.
The head points to the first node. If the list is empty, head is null. All operations start from the head to access or change the list.
Result
You understand that changing the head changes the start of the list.
Recognizing the head as the list's starting point is key to adding or removing nodes efficiently.
3
IntermediateCreating a New Node to Push
🤔Before reading on: do you think the new node should point to null or the current head? Commit to your answer.
Concept: When pushing, the new node must link to the current head to keep the list connected.
To push, create a new node with the data you want. Set its next link to the current head node. This keeps the old list connected after the new node.
Result
The new node points to the old first node, preserving the list chain.
Understanding that the new node links to the old head prevents breaking the list during insertion.
4
IntermediateUpdating Head to New Node
🤔Before reading on: after creating the new node, should the head stay the same or change? Commit to your answer.
Concept: After linking the new node, update the head to point to this new node.
Once the new node points to the old head, set the head variable to the new node. This makes the new node the first in the list.
Result
The head now points to the new node, which leads to the rest of the list.
Knowing to update the head last ensures the list remains accessible and correctly ordered.
5
IntermediateImplementing Push in TypeScript
🤔Before reading on: do you think push should return anything or just update the list? Commit to your answer.
Concept: Push modifies the list by adding a node at the front and updates the head pointer.
class Node { data: number; next: Node | null; constructor(data: number) { this.data = data; this.next = null; } } class LinkedList { head: Node | null = null; push(data: number): void { const newNode = new Node(data); newNode.next = this.head; this.head = newNode; } printList(): void { let current = this.head; let result = ''; while (current) { result += current.data + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const list = new LinkedList(); list.push(10); list.push(20); list.push(30); list.printList();
Result
30 -> 20 -> 10 -> null
Seeing the full code connects the concept to real implementation and output.
6
AdvancedHandling Empty List Push
🤔Before reading on: do you think pushing to an empty list needs special code or works the same? Commit to your answer.
Concept: Pushing to an empty list sets the head to the new node, which points to null.
If the list is empty (head is null), pushing creates a new node whose next is null. Then head points to this new node. This is the base case for push.
Result
The list now has one node, and head points to it.
Knowing the empty list case prevents errors and ensures push works from scratch.
7
ExpertPush Operation Time Complexity and Memory
🤔Before reading on: do you think push takes time proportional to list size or constant time? Commit to your answer.
Concept: Push runs in constant time O(1) because it only changes a few pointers, regardless of list size.
Push creates one node and updates two pointers: newNode.next and head. It does not traverse the list. Memory usage grows by one node each push.
Result
Push is very efficient and suitable for real-time systems needing fast insertions.
Understanding push's O(1) time complexity explains why linked lists are preferred for stack-like operations.
Under the Hood
Internally, push allocates memory for a new node object. It sets the new node's next pointer to the current head node's memory address. Then it updates the head pointer to the new node's address. This pointer manipulation links the new node at the front without moving existing nodes.
Why designed this way?
Linked lists were designed to allow dynamic memory use and fast insertions/removals. Push uses pointer updates to avoid shifting data like arrays. This design trades random access speed for insertion speed and memory flexibility.
Head
  ↓
+---------+      +---------+      +---------+
| NewNode | ---> | OldNode | ---> | ...     | ---> null
+---------+      +---------+      +---------+
Myth Busters - 3 Common Misconceptions
Quick: Does pushing a node add it at the end of the list? Commit yes or no.
Common Belief:Pushing adds a node at the end of the linked list.
Tap to reveal reality
Reality:Pushing always adds a node at the start (head) of the list, not the end.
Why it matters:Mistaking push for append causes inefficient code and bugs when order matters.
Quick: After push, does the old head node get deleted? Commit yes or no.
Common Belief:The old head node is replaced or removed when pushing a new node.
Tap to reveal reality
Reality:The old head node remains intact and becomes the second node after push.
Why it matters:Thinking the old head is lost leads to data loss bugs and misunderstanding list structure.
Quick: Is push operation time dependent on list size? Commit yes or no.
Common Belief:Push takes longer as the list grows because it must find the end.
Tap to reveal reality
Reality:Push runs in constant time O(1) because it only changes the head pointer and new node's next.
Why it matters:Misunderstanding push's efficiency can lead to poor data structure choices.
Expert Zone
1
Push operation does not require traversal, but append does, making push faster for front insertions.
2
In concurrent environments, push requires careful synchronization to avoid race conditions on the head pointer.
3
Memory fragmentation can occur if many nodes are pushed and popped frequently, affecting performance.
When NOT to use
Push is not suitable when you need to add nodes at the end or in the middle; use append or insert methods instead. For random access, arrays or balanced trees are better.
Production Patterns
Push is commonly used to implement stacks, undo buffers, and recursive algorithms where last-in-first-out order is needed. It is also used in functional programming linked list constructions.
Connections
Stack Data Structure
Push in linked lists is the core operation for stack push.
Understanding push in linked lists clarifies how stacks efficiently add elements at the top.
Pointer Manipulation in C
Push relies on pointer updates similar to manual memory management in C.
Knowing pointer basics in C helps grasp how linked lists manage memory and links.
Supply Chain Management
Both involve adding new items at the start of a process chain to prioritize them.
Recognizing push as prioritizing new elements helps understand workflows in logistics and operations.
Common Pitfalls
#1Forgetting to update the head after creating the new node.
Wrong approach:push(data: number): void { const newNode = new Node(data); newNode.next = this.head; // head not updated }
Correct approach:push(data: number): void { const newNode = new Node(data); newNode.next = this.head; this.head = newNode; }
Root cause:Not realizing the head pointer must move to the new node to keep the list connected.
#2Setting new node's next to null instead of current head.
Wrong approach:push(data: number): void { const newNode = new Node(data); newNode.next = null; // breaks list this.head = newNode; }
Correct approach:push(data: number): void { const newNode = new Node(data); newNode.next = this.head; this.head = newNode; }
Root cause:Misunderstanding that new node must link to existing list to avoid losing nodes.
Key Takeaways
Push adds a new node at the start of a linked list by updating pointers.
The new node points to the old head, and then head updates to the new node.
Push runs in constant time, making it very efficient for front insertions.
Handling the empty list case correctly ensures push works from scratch.
Misunderstanding push can cause bugs like broken lists or data loss.

Practice

(1/5)
1. What does the push operation do in a linked list when using nodes?
easy
A. Adds a new node at the beginning of the list
B. Removes the last node from the list
C. Searches for a node with a specific value
D. Deletes the first node of the list

Solution

  1. Step 1: Understand push operation in linked list

    The push operation adds a new node to the start (head) of the linked list.
  2. Step 2: Effect of push on linked list

    The new node becomes the new head, pointing to the previous first node.
  3. Final Answer:

    Adds a new node at the beginning of the list -> Option A
  4. Quick Check:

    Push means add at head = D [OK]
Hint: Push always adds node at the start of the list [OK]
Common Mistakes:
  • Confusing push with pop (removal)
  • Thinking push adds at the end
  • Mixing push with search or delete
2. Which of the following is the correct TypeScript syntax to create a new node and push it to the front of a singly linked list?
easy
A. newNode = new Node(value); head = newNode.next;
B. const newNode = Node(value); head.next = newNode; head = newNode;
C. const newNode = new Node(value); newNode.next = head; head = newNode;
D. head = new Node(value); head.next = null;

Solution

  1. Step 1: Create a new node with given value

    Use new Node(value) to create a node instance.
  2. Step 2: Link new node to current head and update head

    Set newNode.next = head and then head = newNode to push at front.
  3. Final Answer:

    const newNode = new Node(value); newNode.next = head; head = newNode; -> Option C
  4. Quick Check:

    New node points to old head, head updated = A [OK]
Hint: New node next points to head, then head updated [OK]
Common Mistakes:
  • Forgetting to link new node's next to old head
  • Assigning head.next instead of newNode.next
  • Not updating head to new node
3. What will be the linked list after executing this TypeScript code?
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);
medium
A. null -> 10 -> 20 -> 30
B. 10 -> 20 -> 30 -> null
C. 20 -> 30 -> 10 -> null
D. 30 -> 20 -> 10 -> null

Solution

  1. Step 1: Push 10 to empty list

    Head becomes node with value 10, next is null.
  2. Step 2: Push 20 and then 30

    Each new node points to previous head, so order is 30 -> 20 -> 10 -> null.
  3. Final Answer:

    30 -> 20 -> 10 -> null -> Option D
  4. Quick Check:

    Last pushed is head = A [OK]
Hint: Last pushed node is always new head [OK]
Common Mistakes:
  • Assuming push adds at tail
  • Reading list in wrong order
  • Confusing next pointers
4. Identify the error in this push function for a singly linked list:
function push(value: number) {
  const newNode = new Node(value);
  head.next = newNode;
  head = newNode;
}
medium
A. head might be null causing runtime error
B. head.next = newNode is missing
C. head is assigned before linking newNode
D. Node constructor is called incorrectly

Solution

  1. Step 1: Check head usage before assignment

    The code uses head.next without checking if head is null.
  2. Step 2: Consequence of head being null

    If list is empty (head null), accessing head.next causes runtime error.
  3. Final Answer:

    head might be null causing runtime error -> Option A
  4. Quick Check:

    Accessing head.next without null check = B [OK]
Hint: Always check if head is null before accessing head.next [OK]
Common Mistakes:
  • Not checking for empty list before push
  • Forgetting to set newNode.next
  • Assigning head incorrectly
5. Given a linked list with nodes containing values [5, 15, 25] (5 is head), what will be the list after pushing 35 and then 45 using the push function?
Also, explain how the head changes after each push.
hard
A. 35 -> 45 -> 5 -> 15 -> 25 -> null; head moves to 35 then 45
B. 45 -> 35 -> 5 -> 15 -> 25 -> null; head moves to newest node each push
C. 5 -> 15 -> 25 -> 35 -> 45 -> null; head stays at 5
D. null; list is cleared after push

Solution

  1. Step 1: Initial list and push 35

    Original head is 5. Push 35 creates new node 35 pointing to 5. Head updates to 35.
  2. 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.
  3. Final Answer:

    45 -> 35 -> 5 -> 15 -> 25 -> null; head moves to newest node each push -> Option B
  4. Quick Check:

    Push adds at front, head updates = C [OK]
Hint: Head always points to last pushed node [OK]
Common Mistakes:
  • Assuming push adds at tail
  • Not updating head after push
  • Confusing node order after pushes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard