📖 Scenario: Imagine you have a line of friends waiting to get snacks. Each friend holds a card with their name. Sometimes, the first friend in line leaves, and the line shifts forward. This is like deleting the first node in a linked list.
🎯 Goal: You will create a simple linked list with three nodes, then remove the first node, and finally print the linked list to see the updated order.
📋 What You'll Learn
Create a linked list with nodes containing values 10, 20, and 30
Create a variable called head pointing to the first node
Delete the first node by updating head
Print the linked list values in order separated by ' -> ' and ending with ' -> null'
💡 Why This Matters
🌍 Real World
Linked lists are used in many applications like music playlists, undo functionality in apps, and managing tasks where order and quick insertions/deletions matter.
💼 Career
Understanding linked list operations like deleting nodes is fundamental for software developers working with data structures, improving problem-solving and coding skills.
Progress0 / 4 steps
1
Create the initial linked list
Create three nodes as objects named node1, node2, and node3 with values 10, 20, and 30 respectively. Link them by setting the next property so that node1.next is node2 and node2.next is node3. Set node3.next to null. Finally, create a variable called head and assign it to node1.
DSA Javascript
Hint
Think of each node as an object with value and next properties. Link them by setting next to the next node or null if none.
2
Prepare to delete the first node
Create a variable called deletedNode and assign it the current head node. This will hold the node that will be removed.
DSA Javascript
Hint
Store the current first node in a variable before changing the head.
3
Delete the first node by updating head
Update the head variable to point to the next node after the current head by setting head = head.next.
DSA Javascript
Hint
Move the head pointer forward to the second node to remove the first node from the list.
4
Print the linked list after deletion
Create a variable current set to head and an empty string variable output. Use a while loop that runs while current !== null. Inside the loop, append current.value + ' -> ' to output, then update current = current.next. After the loop, append 'null' to output and use console.log(output) to print it.
DSA Javascript
Hint
Traverse from head to the end, building a string of values separated by ' -> ', then print it.
Practice
(1/5)
1. What happens to the head pointer when you delete the first node in a singly linked list?
easy
A. It moves to the next node in the list
B. It becomes null immediately
C. It points to the last node
D. It remains unchanged
Solution
Step 1: Understand the head pointer role
The head pointer points to the first node in the list.
Step 2: Effect of deleting the first node
Deleting the first node means the head must now point to the second node, which becomes the new first node.
Final Answer:
It moves to the next node in the list -> Option A
Quick Check:
Head points to next node after deletion [OK]
Hint: Head always moves to next node when deleting first [OK]
Common Mistakes:
Assuming head becomes null even if list has more nodes
Thinking head points to last node after deletion
Not updating head pointer at all
2. Which of the following is the correct syntax to delete the first node in a singly linked list in JavaScript?
easy
A. delete head;
B. head.next = head;
C. head = head.next;
D. head = null.next;
Solution
Step 1: Understand how to move head pointer
To delete the first node, assign head to head.next to skip the first node.
Step 2: Check each option
head = head.next; correctly assigns head to head.next. head.next = head; reverses pointers incorrectly. delete head; deletes the variable but not the node. head = null.next; is invalid syntax.
Final Answer:
head = head.next; -> Option C
Quick Check:
Assign head to head.next to delete first node [OK]
Hint: Use head = head.next to remove first node [OK]
Common Mistakes:
Using delete keyword incorrectly
Assigning head.next to head instead of head to head.next
Using invalid syntax like null.next
3. Consider the following linked list code in JavaScript:
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
let head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
// Delete first node
head = head.next;
// Print list
let current = head;
let result = [];
while(current) {
result.push(current.data);
current = current.next;
}
console.log(result);
What will be printed?
medium
A. []
B. [10, 20, 30]
C. [30]
D. [20, 30]
Solution
Step 1: Initial linked list nodes
The list has nodes with data 10 -> 20 -> 30.
Step 2: Deleting first node by head = head.next
Head moves from node 10 to node 20, so node 10 is removed.
Step 3: Printing remaining nodes
Starting from new head (20), the list is 20 -> 30, so result array is [20, 30].
Final Answer:
[20, 30] -> Option D
Quick Check:
After deletion, list starts at 20 [OK]
Hint: Deleting first node skips first data in output [OK]
Common Mistakes:
Printing original list without deletion
Confusing head.next with head
Forgetting to update head pointer
4. The following code attempts to delete the first node of a linked list but causes an error:
head.next = head.next.next;
What is the problem with this code if head points to the first node?
medium
A. It deletes the second node instead of the first node
B. It causes a runtime error if head.next is null
C. It correctly deletes the first node
D. It sets head to null incorrectly
Solution
Step 1: Analyze what head.next = head.next.next does
This changes the next pointer of the first node to skip the second node, effectively deleting the second node.
Step 2: Check if first node is deleted
The head pointer still points to the first node, so the first node is not deleted.
Final Answer:
It deletes the second node instead of the first node -> Option A
Quick Check:
head.next changes second node, not head itself [OK]
Hint: Changing head.next skips second node, not first [OK]
Common Mistakes:
Thinking this deletes the first node
Ignoring possible null pointer if list has one node
Confusing head and head.next pointers
5. You have a singly linked list with nodes: 5 -> 10 -> 15 -> 20. Write a function in JavaScript to delete the first node and return the new head. What will be the list after deletion?
hard
A. 20
B. 10 -> 15 -> 20
C. 15 -> 20
D. 5 -> 10 -> 15 -> 20
Solution
Step 1: Write function to delete first node
Function returns head.next to skip first node: function deleteFirstNode(head) { return head ? head.next : null; }
Step 2: Apply function to list 5 -> 10 -> 15 -> 20
New head points to node with data 10, so list becomes 10 -> 15 -> 20.
Final Answer:
10 -> 15 -> 20 -> Option B
Quick Check:
Deleting first node moves head to second node [OK]
Hint: Return head.next to delete first node [OK]
Common Mistakes:
Returning head instead of head.next
Not handling empty list (null head)
Deleting wrong node by changing next pointers incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.