Push Using Linked List Node in DSA Javascript - Time & Space Complexity
We want to understand how long it takes to add a new item to the start of a linked list.
How does the time needed change as the list grows?
Analyze the time complexity of the following code snippet.
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
}
push(value) {
const newNode = new Node(value);
newNode.next = this.head;
this.head = newNode;
}
}
This code adds a new node at the start of the linked list by adjusting pointers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating a new node and updating pointers.
- How many times: Exactly once per push call, no loops or recursion involved.
Adding a node always takes the same steps, no matter how big the list is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 (create node, set next, update head) |
| 100 | 3 (same steps, no change) |
| 1000 | 3 (still the same fixed steps) |
Pattern observation: The number of steps stays the same regardless of list size.
Time Complexity: O(1)
This means adding a node at the start takes a constant amount of time no matter how big the list is.
[X] Wrong: "Adding a node takes longer as the list grows because we have to move through the list."
[OK] Correct: We only change the head pointer and the new node's next pointer; we never traverse the list, so time stays constant.
Knowing that pushing to the front of a linked list is fast and constant time shows you understand efficient data structure operations.
"What if we changed push to add a node at the end of the linked list? How would the time complexity change?"
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: Compare with other operations
Removing or searching nodes are different operations, not push.Final Answer:
Adds a new node at the beginning of the list -> Option BQuick Check:
Push = Add at head [OK]
- Confusing push with pop or delete
- Thinking push adds at the end
- Mixing push with search operation
Solution
Step 1: Create new node and link it to current head
We create a new node, set its next to current head, then update head to new node.Step 2: Check each option
let newNode = new Node(value); newNode.next = head; head = newNode; correctly does this. Others either reverse assignments or miss new keyword.Final Answer:
let newNode = new Node(value); newNode.next = head; head = newNode; -> Option CQuick Check:
New node points to old head, head updated [OK]
- Forgetting to use 'new' keyword
- Assigning head before setting newNode.next
- Mixing order of assignments
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
let head = null;
function push(value) {
let newNode = new Node(value);
newNode.next = head;
head = newNode;
}
push(10);
push(20);
push(30);Solution
Step 1: Trace push calls and list updates
First push(10): head = 10 -> null
Second push(20): head = 20 -> 10 -> null
Third push(30): head = 30 -> 20 -> 10 -> nullStep 2: Confirm final linked list order
New nodes are added at front, so list is 30 -> 20 -> 10 -> null.Final Answer:
30 -> 20 -> 10 -> null -> Option DQuick Check:
Last pushed is first in list [OK]
- Assuming push adds at end
- Confusing order of nodes
- Ignoring that head updates each push
function push(value) {
let newNode = Node(value);
newNode.next = head;
head = newNode;
}Solution
Step 1: Check node creation syntax
Node is a class; must use 'new' keyword to create instance. Missing here.Step 2: Verify other assignments
newNode.next = head and head = newNode are correct order and assignments.Final Answer:
Missing 'new' keyword when creating newNode -> Option AQuick Check:
Use 'new' to create class instance [OK]
- Forgetting 'new' causes runtime error
- Swapping assignment order
- Expecting push to return head
function push(value) {
let newNode = new Node(value);
newNode.next = head;
head = newNode;
}Assume
head points to the first node (5).Solution
Step 1: Understand push adds node at front
Push creates new node with value 10, sets its next to current head (5), then updates head to new node.Step 2: Visualize new list order
New head is 10, followed by old list 5 -> 15 -> 25 -> null, so final list is 10 -> 5 -> 15 -> 25 -> null.Final Answer:
10 -> 5 -> 15 -> 25 -> null -> Option AQuick Check:
Push adds new node at front [OK]
- Appending new node at end instead of front
- Not updating head pointer
- Mixing node order after push
Ready to prove it?
Pass the test and this pattern moves unknown
