Learn basic concept of c , c++ and python programming in regularcodes.com

Spread the post

Move middle node at beginning of given linked list

Suppose given linked list contain following nodes[11,12,13,14,15,16,17].

move middle node at beginning

View insertion process.

Try it yourself


Step 1: We need to find previous node of middle in given linked list. this process will take o(n) time. use two pointer temp and helper.

process to find middle of linked list.

root_ptr17161514 temp13helper 1211

temp pointer are point to middle node and helper are point to previous of middle node.

Step 2: helper->next=temp->next;

root_ptr17161514 temp13helper 1211

Modifying pointer next node address.

Step 3: temp->next=root;

root_ptr17161514 temp13helper 1211

middle node of linked list that next pointer is assign address of first node.

step 4: root=temp;

root_ptr17 161514 temp13helper 1211

root node pointed to middle of linked list.

Visualization and code execution process:

Stack Areamainroot(pointer)move_noderoot(pointer) Heap Areastruct Nodedata (int)= 11next (pointer) struct Nodedata (int)= 12next (pointer) struct Nodedata (int)= 13next (pointer) struct Nodedata (int)= 14next (pointer) struct Nodedata (int)= 15next (pointer) struct Nodedata (int)= 16next (pointer) struct Nodedata (int)= 17next (pointer)= NULL

Note that not given all step of execution process here.View How to insert linked list element, how to print and how to move middle node at head.

Try it yourself

c program move middle node at beginning of linked list.


Spread the post

Recommended Posts: