Linked List MCQ : Time Complexity [Multiple Choice Questions]
admin2013-06-02T03:29:40+00:00Consider the following Chart for Reference :
[table border="1"] Operation,Array,Singly Linked List
Read [any where], O[1] , O[n]
Add/Remove at end, O[1] , O[n]
Add/Remove in the interior , O[n], O[n]
Resize ,O[n] , N/A
Find By position, O[1] , O[n]
Find By target [value], O[n] , O[n]
[/table]
Get the full detail of question consider a linked list of n elements. What is the time taken to insert an element an after element pointed by some pointer?. Here at Quizzcreator we have millions of questions and quizzes, So Play this quiz from here at get the full result.
consider a linked list of n elements. What is the time taken to insert an element an after element pointed by some pointer? is related to "Subset Sum Problem Quiz Question". Here you can create your own quiz and questions like consider a linked list of n elements. What is the time taken to insert an element an after element pointed by some pointer? also and share with your friends. These questions will build your knowledge and your own create quiz will build yours and others people knowledge.
People love to play "Subset Sum Problem Quiz Question" So land here via consider a linked list of n elements. What is the time taken to insert an element an after element pointed by some pointer? Subset Sum Problem Quiz Question now play this via selecting your answer on consider a linked list of n elements. What is the time taken to insert an element an after element pointed by some pointer?.
Here at Quizzcreator This is the best questions and answer library. Here You can enjoy both Questions like consider a linked list of n elements. What is the time taken to insert an element an after element pointed by some pointer? Subset Sum Problem Quiz Question and some related quiz to play like Subset Sum Problem Quiz Question.
consider a linked list of n elements. What is the time taken to insert an element an after element pointed by some pointer? Subset Sum Problem Quiz Question improve the knowledge and give you the fun to play.
Insert N elements in a Linked List one after other at middle position
Given an array of N elements. The task is to insert the given elements at the middle position in the linked list one after another. Each insert operation should take O[1] time complexity.
Examples:
Input: arr[] = {1, 2, 3, 4, 5}
Output: 1 -> 3 -> 5 -> 4 -> 2 -> NULL
1 -> NULL
1 -> 2 -> NULL
1 -> 3 -> 2 -> NULL
1 -> 3 -> 4 -> 2 -> NULL
1 -> 3 -> 5 -> 4 -> 2 -> NULL
Input: arr[] = {5, 4, 1, 2}
Output: 5 -> 1 -> 2 -> 4 -> NULL
Recommended: Please try your approach on {IDE} first, before moving on to the solution.
Approach: There are two cases:
- Number of elements present in the list are less than 2.
- Number of elements present in the list are more than 2.
- The number of elements already present are even say N then the new element is inserted in the middle position that is [N / 2] + 1.
- The number of elements already present are odd then the new element is inserted next to the current middle element that is [N / 2] + 2.
We take one additional pointer ‘middle’ which stores the address of current middle element and a counter which counts the total number of elements.
If the elements already present in the linked list are less than 2 then middle will always point to the first position and we insert the new node after the current middle.
If the elements already present in the linked list are more than 2 then we insert the new node next to the current middle and increment the counter.
If there are an odd number of elements after insertion then the middle points to the newly inserted node else there is no change in the middle pointer.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include using namespace std; // Node structure struct Node { int value; struct Node* next; }; // Class to represent a node // of the linked list class LinkedList { private: struct Node *head, *mid; int count; public: LinkedList[]; void insertAtMiddle[int]; void show[]; }; LinkedList::LinkedList[] { head = NULL; mid = NULL; count = 0; } // Function to insert a node in // the middle of the linked list void LinkedList::insertAtMiddle[int n] { struct Node* temp = new struct Node[]; struct Node* temp1; temp->next = NULL; temp->value = n; // If the number of elements // already present are less than 2 if [count < 2] { if [head == NULL] { head = temp; } else { temp1 = head; temp1->next = temp; } count++; // mid points to first element mid = head; } // If the number of elements already present // are greater than 2 else { temp->next = mid->next; mid->next = temp; count++; // If number of elements after insertion // are odd if [count % 2 != 0] { // mid points to the newly // inserted node mid = mid->next; } } } // Function to print the nodes // of the linked list void LinkedList::show[] { struct Node* temp; temp = head; // Initializing temp to head // Iterating and printing till // The end of linked list // That is, till temp is null while [temp != NULL] { cout value next; } cout 3 -> 5 -> 4 -> 2 -> NULL Time Complexity : O[N] Article Tags : Data Structures Linked List Mathematical Practice Tags : Data Structures Linked List Mathematical Read Full Article Linked List | Set 2 [Inserting a node]
We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal. C++
C
Java
Python
C#
Javascript
In this post, methods to insert a new node in linked list are discussed. A node can be added in three ways
Video liên quanChủ Đề |