Move first node a middle of given linked list
Suppose following data [1, 2, 3, 4, 5, 6, 7, 8] are inserted on linked list.
Following process to move first node at middle of linked list.
1) Use two pointer temp and helper. temp pointer and helper pointer are pointing to first node of linked list.
2) First find middle node of linked list.
3) move root pointer to second node of linked list.
root=root->next;
4) helper=temp->next;
First node of linked list that (next pointer) value are pointed to middle node of next node pointer.
5) temp->next=helper;
And finally middle node pointed to next node of this helper pointer.
Algorithm:
Time complexity of this algorithm O(n).
Code Execution:
Note that not given all step of execution process here.View How to insert linked list element, how to print and so on.
c program to move first node at middle of linked list.
Case 1: Empty Linked list Empty linked List case 2: When linked list are not empty Before move linked list :1 2 3 4 5 6 7 8 After move linked list :2 3 4 1 5 6 7 8 Free nodes of linked linked
View comments and participate Discussion