Answer (Detailed Solution Below) Option 3 : 2 pointers
20 Qs. 20 Marks 15 Mins
Circular linked list: Example Let x be a variable pointer and head. Head is the pointer of the circular linked list. Now to insert a new record (node) y, we have to loop through the linked list from the head to the last node like this pseudocode below. x=head while(x->next!=head) { x=x->next; } After finishing the loop x is now the last node and we will append the list. y is the next node of x Then the next of y will have to point to the head since the linked list is circular. The pseudocode for this task is below. x->next=y; y->next=head; So There needs modification of two pointers. So the correct answer is Option 3 India’s #1 Learning Platform Start Complete Exam Preparation
Video Lessons & PDF Notes Trusted by 2,99,79,859+ Students
We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal.
In this post, methods to insert a new node in linked list are discussed. A node can be added in three ways Add a node at the front: (4 steps process) Following are the 4 steps to add a node at the front.
We have a pointer to the head and we can directly attach a node and change the pointer. So the Time complexity of inserting a node at head position is O(1) as it does a constant amount of work. Add a node after a given node: (5 steps process) We are given a pointer to a node, and the new node is inserted after the given node.
Time complexity of insertAfter() is O(n) as it depends on n where n is the size of the linked list Space complexity: O(1) since using constant space to modify pointers Add a node at the end: (6 steps process) The new node is always added after the last node of the given Linked List. For example if the given Linked List is 5->10->15->20->25 and we add an item 30 at the end, then the Linked List becomes 5->10->15->20->25->30. Since a Linked List is typically represented by the head of it, we have to traverse the list till the end and then change the next to last node to a new node. Following are the 6 steps to add node at the end.
Time complexity of append is O(n) where n is the number of nodes in the linked list. Since there is a loop from head to end, the function does O(n) work. Following is a complete program that uses all of the above methods to create a linked list.
Output: Created Linked list is: 1 7 8 6 4Time Complexity : O(n) Alternate method by using constructor callHowever there is another method which uses constructor call inside the node class in order to minimize the memory allocation work. It also minimizes the number of lines of code.
Output After insertion at head: 2 -> 1 -> NULL After insertion at tail: 2 -> 1 -> 4 -> 5 -> NULL After insertion at a given position: 2 -> 1 -> 2 -> 4 -> 5 -> 6 -> NULL Time Complexity : O(n) You may like to try Practice MCQ Questions on Linked List Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Article Tags : Practice Tags : |