w3resource

C++ Linked List: Exercises, Practice, Solution


This resource offers a total of 105 C++ Linked List problems for practice. It includes 21 main exercises, each accompanied by solutions, detailed explanations, and four related problems.

[An Editor is available at the bottom of the page to write and execute the scripts.]


1. Create and Display Singly Linked List

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
Click me to see the sample solution


2. Display Singly Linked List in Reverse Order

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
Click me to see the sample solution


3. Count Nodes in a Singly Linked List

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
Click me to see the sample solution

4. Insert Node at the Beginning of Singly Linked List

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
Click me to see the sample solution


5. Insert Node at the End of Singly Linked List

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
Click me to see the sample solution


6. Find the Middle Element of a Linked List

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
Click me to see the sample solution


7. Insert Node in the Middle of Singly Linked List

Write a C++ program to insert a new node at the middle of a given Singly Linked List.

Test Data:
Original list:
7 5 3 1
Singly Linked List: after insert 9 in the middle of the said list-
7 5 9 3 1
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
Click me to see the sample solution


8. Get Nth Node in a Singly Linked List

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
Click me to see the sample solution


9. Insert Node at Any Position in Singly Linked List

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
Click me to see the sample solution


10. Delete First Node of Singly Linked List

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
Click me to see the sample solution


11. Delete Node from the Middle of Singly Linked List

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
Click me to see the sample solution


12. Delete the Last Node of a Singly Linked List

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
Click me to see the sample solution


13. Delete Nth Node from the End of Singly Linked List

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
Click me to see the sample solution


14. Find kth Node by Starting at Middle and Moving Towards Head

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
Click me to see the sample solution


15. Create and Display Doubly Linked List

Write a C++ program to create and display a doubly linked list.

Test Data:
Doubly linked list is as follows:
Traversal in Forward direction:
Orange White Green Red
Traversal in Reverse direction:
Red Green White Orange
Click me to see the sample solution


16. Display Doubly Linked List in Reverse Order

Write a C++ program to create a doubly linked list of n nodes and display it in reverse order.

Test Data:
Doubly linked list is as follows:
Traversal in Forward direction:
Orange White Green Red
Traversal in Reverse direction:
Red Green White Orange
Reverse Doubly linked list:
Traversal in Forward direction:
Red Green White Orange
Traversal in Reverse direction:
Orange White Green Red
Click me to see the sample solution


17. Count Nodes in a Doubly Linked List

Write a program in C++ to create a doubly linked list of n nodes and count the number of nodes.

Test Data:
Doubly linked list is as follows:
Traversal in Forward direction:
Orange White Green Red
Traversal in Reverse direction:
Red Green White Orange
Total number of nodes = 4
Click me to see the sample solution


18. Insert Node at the Beginning of Doubly Linked List

Write a C++ program to insert a new node at the beginning of a Doubly Linked List.

Test Data:
Doubly linked list is as follows:
------------------------------
Traversal in Forward direction: Orange White Green Red
Traversal in Reverse direction: Red Green White Orange
Insert a new node at the beginning of a Doubly Linked List:
-------------------------------------------------------------
Traversal in Forward direction: Pink Orange White Green Red
Traversal in Reverse direction: Red Green White Orange Pink
Click me to see the sample solution


19. Insert Node at the End of Doubly Linked List

Write a C++ program to insert a new node at the end of a Doubly Linked List.

Test Data:
Doubly linked list is as follows:
------------------------------
Traversal in Forward direction: Orange White Green Red
Traversal in Reverse direction: Red Green White Orange
Insert a new node at the end of a Doubly Linked List:
-------------------------------------------------------------
Traversal in Forward direction: Orange White Green Red Pink
Traversal in Reverse direction: Pink Red Green White Orange
Click me to see the sample solution


20. Find the Middle Element of a Doubly Linked List

Write a C++ program to find the middle element of a given Doubly Linked List.

Test Data:
Doubly linked list is as follows:
------------------------------
Traversal in Forward direction: Orange White Green Red
Traversal in Reverse direction: Red Green White Orange
The middle element is: Green
Insert a new node at the end of a Doubly Linked List:
-------------------------------------------------------------
Traversal in Forward direction: Pink Orange White Green Red
Traversal in Reverse direction: Red Green White Orange Pink
The middle element is: White
Click me to see the sample solution


21. Insert Node at the Middle of Doubly Linked List

Write a C++ program to insert a new node at the middle of a given Doubly Linked List.

Test Data:
Doubly linked list is as follows:
---------------------------------
Traversal in Forward direction: Orange White Green Red
Traversal in Reverse direction: Red Green White Orange
Insert a new node at the middle position of the said Doubly linked list:
-----------------------------------------------------------------------
Traversal in Forward direction: Orange White Pink Green Red
Traversal in Reverse direction: Red Green Pink White Orange
Insert another new node at the middle position of the said Doubly linked list:
-----------------------------------------------------------------------------
Traversal in Forward direction: Orange White Pink Black Green Red
Traversal in Reverse direction: Red Green Black Pink White Orange
Click me to see the sample solution


CPP Code Editor:



More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.