Insert N elements in a Linked List one after other at middle positionGiven 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. Show
Recommended: Please try your approach on {IDE} first, before moving on to the solution. Approach: There are two cases:
We take one additional pointer ‘middle’ which stores the address of current middle element and a counter which counts the total number of elements. C++
Java
Python3
C#
Javascript
Output:
1 -> 3 -> 5 -> 4 -> 2 -> NULL
Time Complexity : O(N)
Article Tags :
Data Structures Linked List Mathematical
Practice Tags :
Data Structures Linked List Mathematical Singly Linked List vs Doubly Linked ListBefore looking at the differences between the singly linked list and doubly linked list, we first understand what is singly linked list and doubly linked list separately. |