Introductions
Singly linked lists are one of the most important linked list types to practice because each node points only to the next node. These questions focus on practical operations such as creating nodes, traversing the list, searching, inserting, deleting, counting nodes, finding the middle node, and reversing a singly linked list. The examples use JavaScript and gradually move from basic operations to common problem-solving patterns. Data Structure Singly Linked List practice questions with solutions help to understand the concepts.
Question 1: Create a Singly Linked List
Question
Create a singly linked list containing:
10 → 20 → 30 → null
Print all the elements.
Solution
In a singly linked list, every node contains two parts:
Data | Next
The next property stores the reference to the next node.
Create a Node class:
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
Now create the nodes:
let head = new Node(10);
let second = new Node(20);
let third = new Node(30);
Connect them:
head.next = second;
second.next = third;
The list is now:
10 → 20 → 30 → null
To print the elements:
let current = head;
while (current !== null) {
console.log(current.data);
current = current.next;
}
Complete code:
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
let head = new Node(10);
let second = new Node(20);
let third = new Node(30);
head.next = second;
second.next = third;
let current = head;
while (current !== null) {
console.log(current.data);
current = current.next;
}
Output
10
20
30
Answer
The singly linked list is:
10 → 20 → 30 → null
Question 2: Traverse a Singly Linked List
Question
Print every element of this singly linked list:
5 → 10 → 15 → 20 → 25 → null
Solution
Start from the head node.
Then move to the next node using:
current = current.next;
Continue until current becomes null.
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
let head = new Node(5);
head.next = new Node(10);
head.next.next = new Node(15);
head.next.next.next = new Node(20);
head.next.next.next.next = new Node(25);
let current = head;
while (current !== null) {
console.log(current.data);
current = current.next;
}
The traversal happens as:
5 → 10 → 15 → 20 → 25 → null
Output
5
10
15
20
25
Answer
Every node is visited from the first node to the last node.
Question 3: Count Nodes in a Singly Linked List
Question
Count the number of nodes in:
10 → 20 → 30 → 40 → null
Solution
Create a counter:
let count = 0;
Every time we visit a node, increase the counter:
count++;
Complete code:
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);
head.next.next.next = new Node(40);
let count = 0;
let current = head;
while (current !== null) {
count++;
current = current.next;
}
console.log(count);
The counting works like this:
10 → count = 1
20 → count = 2
30 → count = 3
40 → count = 4
Output
4
Answer
The singly linked list contains 4 nodes.
Question 4: Search for an Element
Question
Search for 30 in the following singly linked list:
10 → 20 → 30 → 40 → 50 → null
Print the position of the element if it is found.
Solution
We can start the position from 0.
10 → position 0
20 → position 1
30 → position 2
We check every node until we find the target.
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);
head.next.next.next = new Node(40);
head.next.next.next.next = new Node(50);
let target = 30;
let current = head;
let position = 0;
while (current !== null) {
if (current.data === target) {
console.log("Found at position:", position);
break;
}
current = current.next;
position++;
}
The search checks:
10 → Not found
20 → Not found
30 → Found
Output
Found at position: 2
Answer
The value 30 is found at position 2.
Question 5: Insert a Node at the Beginning
Question
Given:
20 → 30 → 40 → null
Insert 10 at the beginning.
Solution
First create a new node:
let newNode = new Node(10);
The new node initially points to null:
10 → null
Now connect it to the current head:
newNode.next = head;
Then make the new node the new head:
head = newNode;
Before insertion:
head
↓
20 → 30 → 40 → null
After insertion:
head
↓
10 → 20 → 30 → 40 → null
Complete code:
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
let head = new Node(20);
head.next = new Node(30);
head.next.next = new Node(40);
let newNode = new Node(10);
newNode.next = head;
head = newNode;
let current = head;
while (current !== null) {
console.log(current.data);
current = current.next;
}
Output
10
20
30
40
Answer
10 is successfully inserted at the beginning.
Question 6: Insert a Node at the End
Question
Given:
10 → 20 → 30 → null
Insert 40 at the end.
Solution
Create the new node:
let newNode = new Node(40);
Now find the last node.
The last node is the node whose next is null.
let current = head;
while (current.next !== null) {
current = current.next;
}
After the loop, current points to 30.
Connect it to the new node:
current.next = newNode;
The list becomes:
10 → 20 → 30 → 40 → null
Complete code:
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);
let newNode = new Node(40);
let current = head;
while (current.next !== null) {
current = current.next;
}
current.next = newNode;
current = head;
while (current !== null) {
console.log(current.data);
current = current.next;
}
Output
10
20
30
40
Answer
40 is successfully inserted at the end of the singly linked list.
Question 7: Insert a Node After a Given Value
Question
Given:
10 → 20 → 40 → 50 → null
Insert 30 after 20.
Solution
We need to find the node containing 20.
Once we find it, we need to adjust two links.
Before insertion:
20 → 40
Create the new node:
30 → null
Connect 30 to 40:
newNode.next = current.next;
Then connect 20 to 30:
current.next = newNode;
The result becomes:
20 → 30 → 40
Complete code:
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(40);
head.next.next.next = new Node(50);
let target = 20;
let newNode = new Node(30);
let current = head;
while (current !== null) {
if (current.data === target) {
newNode.next = current.next;
current.next = newNode;
break;
}
current = current.next;
}
current = head;
while (current !== null) {
console.log(current.data);
current = current.next;
}
Output
10
20
30
40
50
Answer
30 is successfully inserted after 20.
Question 8: Delete the First Node
Question
Delete the first node from:
10 → 20 → 30 → 40 → null
Solution
The first node is pointed to by head.
To delete it, move head to the next node:
head = head.next;
Before deletion:
head
↓
10 → 20 → 30 → 40 → null
After deletion:
head
↓
20 → 30 → 40 → null
Complete code:
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);
head.next.next.next = new Node(40);
head = head.next;
let current = head;
while (current !== null) {
console.log(current.data);
current = current.next;
}
Output
20
30
40
Answer
The first node containing 10 has been removed.
Question 9: Delete a Node by Value
Question
Delete the node containing 30 from:
10 → 20 → 30 → 40 → 50 → null
Solution
To delete 30, we need to find the node before it.
That node is 20.
Before deletion:
20 → 30 → 40
We need to change the link:
20 → 40
In code:
current.next = current.next.next;
Complete code:
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);
head.next.next.next = new Node(40);
head.next.next.next.next = new Node(50);
let target = 30;
let current = head;
while (current !== null && current.next !== null) {
if (current.next.data === target) {
current.next = current.next.next;
break;
}
current = current.next;
}
current = head;
while (current !== null) {
console.log(current.data);
current = current.next;
}
The link changes from:
10 → 20 → 30 → 40 → 50
to:
10 → 20 → 40 → 50
Output
10
20
40
50
Answer
The node containing 30 has been successfully deleted.
Question 10: Reverse a Singly Linked List
Question
Reverse the following singly linked list:
10 → 20 → 30 → 40 → null
The expected result is:
40 → 30 → 20 → 10 → null
Solution
To reverse a singly linked list, we need to change the direction of every next link.
We use three variables:
previous
current
next
Initially:
previous = null
current = head
The list is:
10 → 20 → 30 → 40 → null
Step 1
Current node is 10.
Save the next node:
next = current.next;
Change the direction:
current.next = previous;
Move previous forward:
previous = current;
Move current forward:
current = next;
Now:
null ← 10 20 → 30 → 40 → null
Step 2
Process 20:
null ← 10 ← 20 30 → 40 → null
Step 3
Process 30:
null ← 10 ← 20 ← 30 40 → null
Step 4
Process 40:
null ← 10 ← 20 ← 30 ← 40
Finally, make previous the new head.
head = previous;
Complete code:
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);
head.next.next.next = new Node(40);
let previous = null;
let current = head;
while (current !== null) {
let next = current.next;
current.next = previous;
previous = current;
current = next;
}
head = previous;
let result = head;
while (result !== null) {
console.log(result.data);
result = result.next;
}
The original list:
10 → 20 → 30 → 40 → null
becomes:
40 → 30 → 20 → 10 → null
Output
40
30
20
10
Answer
The singly linked list has been successfully reversed.
Key Takeaways
- A singly linked list contains nodes connected in one direction.
- Each node contains data and a
nextreference. - The
headpoints to the first node. - The last node points to
null. - Traversal starts from
headand continues usingcurrent.next. - Searching a singly linked list requires checking nodes one by one.
- Inserting at the beginning only requires changing the head reference.
- Inserting after a node requires changing the links between neighboring nodes.
- Deleting the first node can be done by setting
head = head.next. - Deleting a middle node requires connecting the previous node to the node after the deleted node.
- A singly linked list can be reversed using
previous,current, andnextpointers. - Traversing or searching a singly linked list generally takes O(n) time.
- Inserting at the beginning of a singly linked list takes O(1) time.
FAQs
What is a singly linked list?
A singly linked list is a collection of nodes where each node stores data and a reference to the next node. The list can be traversed in only one direction.
What is the head in a singly linked list?
The head is a reference to the first node of the singly linked list. Traversal normally starts from the head.
What does the next property store?
The next property stores a reference to the next node. For the last node, next is normally null.
How do you traverse a singly linked list?
Start with the head and repeatedly move to the next node:
current = current.next;
Continue until current becomes null.
How do you insert a node at the beginning?
Create a new node, point its next to the current head, and then make the new node the head.
newNode.next = head;
head = newNode;
How do you delete a node from a singly linked list?
For a node in the middle, find the previous node and change its next reference so that it skips the node being deleted.
What is the time complexity of reversing a singly linked list?
Reversing a singly linked list requires visiting every node once, so its time complexity is O(n).
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
