Insert node at middle of linked list
How to insert data at middle (mid-position) of singly linked list?. Here given following process to insert node.
Process to insert node at middle of linked list
Step 1: Create memory block on heap area.
Step 2: When memory are allocate using malloc function then Assign data value to newly created node.
Step 3: If no element in this linked list then assign this newly created memory block address to root pointer
Step 4: In this process to finding the mid-element of linked list that process time are complexity O(n).
Step 5: When Exist two or more linked list nodes element.
Step 6: If only one element in linked list node.
Visualization :Suppose given linked list is given below. View and editing given this linked list.
Inserting this newly created node 777 on mid-position after linked list is.
Suppose following data [ 44, 35, 34, 144, 23, 36, 66, 62] are inserted step by step.
Initial linked list are empty then insert first element 44.
View nodes and pointer.This is base condition of insert first element of linked list.
Insert second element on middle position. note that only single element are in linked list. then in this case insert node after first node of linked list. insert data 35.
insert another data:34
look at this change next pointer positions.
insert data : 144
Insert another node data:23.
similarly insert remaining data [36, 66, 62]. visualize insert data in animated view
After insert other element linked list.
Code imagination : Insert middle element of linked list that process are given below. step by step code execution process are available here. see and imagize c code execution to insert middle element of linked list.
Time complexity of this program O(n)
Given below C program to insert element at middle of linked list.
Linked list data is : 44 34 23 66 62 36 144 35 Free linked list Node element