C++ Linked List: Exercises, Practice, Solution
1. Write a C++ program to create and display a Singly Linked List.
Test Data:
The list contains the data entered:
11 9 7 5 3 1
2. Write a C++ program to create a singly linked list of n nodes and display
it in reverse order.
Test Data:
Original Linked list: 11 9 7 5 3 1
Reverse Linked list: 1 3 5 7 9 11
3. Write a C++ program to create a singly linked list of n nodes and count
the number of nodes.
Test Data:
Original Linked list: 13 11 9 7 5 3 1
Number of nodes in the said Linked list: 7
4. Write a C++ program to insert a new node at the beginning of a Singly
Linked List.
Test Data:
Original Linked list: 13 11 9 7 5 3 1
Insert a new node at the beginning of a Singly Linked List:
0 13 11 9 7 5 3 1
5. Write a C++ program to insert a new node at the end of a Singly Linked
List.
Test Data:
Original Linked list: 13 11 9 7 5 3 1
Insert a new node at the end of a Singly Linked List: 13 11 9 7 5 3 1 0
6. Write a C++ program to find the middle element of a given Linked List.
Test Data:
Original list: 7 5 3 1
Middle element of the said list: 3
Original list: 9 7 5 3 1
Middle element of the said list: 5
7. Write a C++ program to insert a new node at the middle of a given
Singly Linked List.
Test Data:
Original list:
7531
Singly Linked List: after insert 9 in the middle of the said list-
75931
Singly Linked List: after insert 11 in the middle of the said list-
7 5 9 11 3 1
Singly Linked List: after insert 13 in the middle of the said list-
7 5 9 13 11 3 1
8. Write a C++ program to get Nth node in a given Singly Linked List.
Test Data:
Original list: 7 5 3 1
Position: 1 Value: 7
Position: 2 Value: 5
Position: 3 Value: 3
Position: 4 Value: 1
9. Write a C++ program to insert a new node at any position of a Singly
Linked List.
Test Data:
Original list: 7 5 3 1
Position: 1, Value: 12
Updated list: 12 7 5 3 1
Position: 4, Value: 14
Updated list: 12 7 5 14 3 1
Position: 7, Value: 18
Updated list: 12 7 5 14 3 1 18
10. Write a C++ program to delete first node of a given Singly Linked List.
Test Data:
Original Linked list: 13 11 9 7 5 3 1
Delete first node of Singly Linked List: 11 9 7 5 3 1
11. Write a C++ program to delete a node from the middle of Singly Linked
List.
Test Data:
Original list: 9 7 5 3 1
After removing the middle element of the said list: 9 7 3 1
After removing the middle element of the said list: 9 7 1
After removing the middle element of the said list: 9 1
After removing the middle element of the said list: 9
12. Write a C++ program to delete the last node of a Singly Linked List.
Test Data:
Original list: 7 5 3 1
Remove the last node of the said list: Updated list: 7 5 3
Again remove the last node of the said list: Updated list: 7 5
13. Write a C++ program to delete the nth node of a Singly Linked List from
the end.
Test Data:
Original list: 7 5 3 1
Remove the 2nd node from the end of the said list:
Updated list: 7 5 1
Remove the 3rd node from the end of the said list:
Updated list: 5 1
14. Write a C++ program to find the kth node of a linked list by starting at
the middle and moving towards the head.
Test Data:
Original list: 9 7 5 3 1
kth node of a linked list by starting at the middle and moving towards the
head: Position = 2 Value = 9