📖 Scenario: Imagine you have a chain of friends holding hands in a line. Sometimes, the friend at the end leaves the line. We want to update the line to show who is still holding hands.
🎯 Goal: You will create a simple linked list of friends, then write code to remove the friend at the end of the line, and finally show the updated line.
📋 What You'll Learn
Create a singly linked list with exactly three nodes named 'Alice', 'Bob', and 'Charlie' in that order.
Create a variable called head that points to the first node.
Write code to delete the last node from the linked list.
Print the linked list after deletion in the format: Alice -> Bob -> null.
💡 Why This Matters
🌍 Real World
Linked lists are used in many programs where data needs to be stored in order but can change size, like music playlists or undo history in apps.
💼 Career
Understanding linked lists and how to modify them is a key skill for software developers, especially when working with low-level data or optimizing memory.
Progress0 / 4 steps
1
Create the linked list with three nodes
Create three nodes named node1, node2, and node3. Set their value properties to 'Alice', 'Bob', and 'Charlie' respectively. Link them so that node1.next = node2 and node2.next = node3. Set node3.next = null. Finally, create a variable called head that points to node1.
DSA Javascript
Hint
Think of each node as a friend holding the next friend's hand. The last friend holds no one's hand, so next is null.
2
Create a helper variable to track the current node
Create a variable called current and set it equal to head. This will help us move through the linked list.
DSA Javascript
Hint
We use current to walk through the list without losing the start point head.
3
Delete the last node from the linked list
Write a while loop that moves current forward until current.next.next is null. Then set current.next = null to remove the last node.
DSA Javascript
Hint
We stop at the node before the last one, then cut off the last node by setting next to null.
4
Print the linked list after deletion
Write code to print the linked list starting from head. Use a while loop with a variable temp to go through each node. Print each node's value followed by ' -> '. After the loop, print 'null'.
DSA Javascript
Hint
Print each friend's name followed by an arrow, then end with null to show the line ends.
Practice
(1/5)
1. What does the operation Delete Node at End do in a singly linked list?
easy
A. Adds a new node at the end of the linked list
B. Removes the first node from the linked list
C. Removes the last node from the linked list
D. Adds a new node at the beginning of the linked list
Solution
Step 1: Understand the linked list structure
A singly linked list is a chain of nodes where each node points to the next node, ending with a node pointing to null.
Step 2: Identify what deleting the last node means
Deleting the last node means removing the node that points to null, effectively shortening the list by one node at the end.
Final Answer:
Removes the last node from the linked list -> Option C
Quick Check:
Delete Node at End = Remove last node [OK]
Hint: Last node points to null; delete it to remove end [OK]
Common Mistakes:
Confusing delete at end with delete at start
Thinking it adds a node instead of removing
Assuming it deletes a middle node
2. Which of the following is the correct syntax to delete the last node in a singly linked list in JavaScript?
easy
A. list.deleteEnd();
B. list.pop();
C. list.deleteNodeAtEnd();
D. list.removeLastNode();
Solution
Step 1: Recognize common JavaScript linked list methods
JavaScript arrays use pop() to remove the last element; custom linked lists often implement similar methods.
Step 2: Identify the standard method name
Among the options, pop() is the standard method name used to remove the last element in many list implementations.
Final Answer:
list.pop(); -> Option B
Quick Check:
pop() removes last element [OK]
Hint: pop() usually removes last element in lists [OK]
Common Mistakes:
Using non-standard method names
Confusing delete with remove
Assuming deleteEnd() is built-in
3. Consider the following JavaScript code for a singly linked list deletion at end:
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
}
deleteAtEnd() {
if (!this.head) return null;
if (!this.head.next) {
this.head = null;
return;
}
let current = this.head;
while (current.next.next) {
current = current.next;
}
current.next = null;
}
}
let list = new LinkedList();
list.head = new Node(1);
list.head.next = new Node(2);
list.head.next.next = new Node(3);
list.deleteAtEnd();
let result = [];
let temp = list.head;
while(temp) {
result.push(temp.data);
temp = temp.next;
}
console.log(result);
What will be printed?
medium
A. [1, 2, 3]
B. [2, 3]
C. [1]
D. [1, 2]
Solution
Step 1: Understand the initial list and deletion
The list starts as 1 -> 2 -> 3. The deleteAtEnd() method removes the last node (3).
Step 2: Trace the deletion and final list
After deletion, the list becomes 1 -> 2. The loop collects these values into the array [1, 2].
Final Answer:
[1, 2] -> Option D
Quick Check:
Deleting last node removes 3, leaving [1, 2] [OK]
Hint: Deleting end removes last node, so last value drops [OK]
Common Mistakes:
Forgetting to update the second last node's next to null
Assuming the head changes incorrectly
Not handling single-node list case
4. The following code tries to delete the last node of a singly linked list but causes an error:
deleteAtEnd() {
let current = this.head;
while (current.next) {
current = current.next;
}
current = null;
}
What is the main problem?
medium
A. It sets the local variable to null but does not update the list
B. It does not handle empty list case
C. It deletes the head node instead of last node
D. It causes infinite loop
Solution
Step 1: Analyze the loop and pointer update
The loop moves current to the last node, but setting current = null only changes the local variable, not the list's links.
Step 2: Understand why the last node is not removed
To remove the last node, the previous node's next must be set to null. This code misses that step.
Final Answer:
It sets the local variable to null but does not update the list -> Option A
Quick Check:
Local variable null doesn't remove node [OK]
Hint: Update previous node's next, not local pointer [OK]
Common Mistakes:
Setting current to null instead of previous.next
Not checking for single-node list
Ignoring empty list case
5. You have a singly linked list with nodes: 10 -> 20 -> 30 -> 40. You want to delete the last node and then add a new node with value 50 at the end. Which sequence of operations correctly achieves this?
hard
A. Call deleteAtEnd(), then call addAtEnd(50)
B. Call addAtEnd(50), then call deleteAtEnd()
C. Call deleteAtEnd() twice, then addAtEnd(50)
D. Call deleteAtEnd(), then addAtStart(50)
Solution
Step 1: Delete the last node
Calling deleteAtEnd() removes the node with value 40, list becomes 10 -> 20 -> 30.
Step 2: Add new node at the end
Calling addAtEnd(50) adds a new node with value 50 at the end, list becomes 10 -> 20 -> 30 -> 50.
Final Answer:
Call deleteAtEnd(), then call addAtEnd(50) -> Option A
Quick Check:
Delete last then add new last node [OK]
Hint: Delete last first, then add new node at end [OK]
Common Mistakes:
Adding before deleting causes wrong order
Deleting twice removes too many nodes
Adding at start changes list order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.