📖 Scenario: Imagine you have a line of people waiting to buy tickets. Sometimes, the first person in line leaves before buying. We want to update the line to remove that first person.
🎯 Goal: You will create a singly linked list with three nodes, then remove the first node (person) from the list, and finally print the updated list.
📋 What You'll Learn
Create a singly linked list with nodes containing values 10, 20, and 30 in that order.
Create a variable called head that points to the first node.
Write code to delete the first node by updating head.
Print the linked list nodes from head to the end in the format: 10 -> 20 -> 30 -> null
💡 Why This Matters
🌍 Real World
Linked lists are used in many applications like music playlists, undo functionality in apps, and managing tasks in operating systems.
💼 Career
Understanding linked list operations like deletion is fundamental for software developers working with data structures and memory management.
Progress0 / 4 steps
1
Create the Singly Linked List
Create a class called Node with an integer data and a Node reference called next. Then create three nodes with values 10, 20, and 30. Link them so that node1 points to node2, and node2 points to node3. Finally, create a variable called head that points to node1.
DSA Java
Hint
Remember to create the Node class first, then create nodes and link them by setting next.
2
Prepare to Delete the First Node
Inside the main method, create a variable called temp and set it equal to head. This will help us track the current list before deletion.
DSA Java
Hint
Use Node temp = head; to keep a reference to the current list.
3
Delete the First Node
Update the head variable to point to the second node by setting head = head.next. This removes the first node from the list.
DSA Java
Hint
Set head = head.next; to remove the first node.
4
Print the Updated Linked List
Use a while loop with a variable current starting at head. Inside the loop, print current.data followed by " -> ". Move current to current.next each time. After the loop, print "null".
DSA Java
Hint
Use a while loop to print each node's data followed by " -> ", then print "null" at the end.
Practice
(1/5)
1. What happens to the head pointer when you delete a node at the beginning of 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 always points to the first node in the list.
Step 2: Effect of deleting the first node
Deleting the first node means the head must 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 points to first node; update it to next after deletion [OK]
Common Mistakes:
Forgetting to update head pointer
Setting head to null without checking list
Pointing head to last node incorrectly
2. Which of the following is the correct syntax to delete the first node in a singly linked list in Java?
easy
A. head = head.next;
B. head.next = head;
C. head = null.next;
D. head.next = null;
Solution
Step 1: Understand the head pointer update
To delete the first node, head must point to the second node, which is head.next.
Step 2: Check each option
head = head.next; correctly assigns head to head.next. Others either cause errors or wrong assignments.
Final Answer:
head = head.next; -> Option A
Quick Check:
Assign head to next node to delete first [OK]
Hint: Assign head to head.next to delete first node [OK]
Common Mistakes:
Using head.next = head which reverses links
Trying to access null.next causing error
Setting head.next to null which breaks list
3. Given the linked list: 10 -> 20 -> 30 -> null, what will be the list after deleting the node at the beginning using head = head.next;?
medium
A. 10 -> 20 -> 30 -> null
B. 20 -> 30 -> null
C. 30 -> null
D. null
Solution
Step 1: Identify the initial list
The list starts as 10 -> 20 -> 30 -> null, with head pointing to 10.
Step 2: Apply deletion by moving head
Setting head = head.next moves head to 20, so the list becomes 20 -> 30 -> null.
Final Answer:
20 -> 30 -> null -> Option B
Quick Check:
Head moves from 10 to 20 after deletion [OK]
Hint: After deletion, head points to second node, list starts there [OK]
Common Mistakes:
Not updating head pointer
Assuming first node remains
Confusing node values after deletion
4. What is wrong with this code snippet for deleting the first node in a singly linked list?
if (head == null) {
System.out.println("List is empty");
} else {
head.next = head;
}
medium
A. It correctly deletes the first node
B. It throws a NullPointerException
C. It sets head to null, losing the list
D. It causes the head to point to itself, creating a cycle
Solution
Step 1: Analyze the assignment head.next = head
This sets the next pointer of the first node to point back to itself, creating a cycle.
Step 2: Understand the impact on deletion
The head pointer is not updated, so the first node is not removed and the list structure is broken.
Final Answer:
It causes the head to point to itself, creating a cycle -> Option D
Quick Check:
Incorrect pointer assignment creates cycle [OK]
Hint: Assign head = head.next to delete; avoid head.next = head [OK]
Common Mistakes:
Confusing head.next = head with head = head.next
Not checking for empty list before deletion
Ignoring pointer updates causing cycles
5. You have a singly linked list with nodes: 5 -> 15 -> 25 -> 35 -> null. After deleting the first node twice using head = head.next;, what will be the final list?
hard
A. 35 -> null
B. 15 -> 25 -> 35 -> null
C. 25 -> 35 -> null
D. null
Solution
Step 1: Initial list and first deletion
Start: 5 -> 15 -> 25 -> 35 -> null. After first deletion, head moves to 15, list is 15 -> 25 -> 35 -> null.
Step 2: Second deletion
After second deletion, head moves to 25, list is 25 -> 35 -> null.
Final Answer:
25 -> 35 -> null -> Option C
Quick Check:
Two deletions remove first two nodes [OK]
Hint: Each deletion moves head to next node, removing first node [OK]
Common Mistakes:
Deleting only once instead of twice
Confusing node values after multiple deletions
Not updating head pointer correctly each time
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.