Spread the post

Move last node to front of given linked list

move last node at front of linked list. First need to find that particular node. Suppose following data [414, 35, 34, 75, 213, 36, 66, 162] are inserted on linked list.

Given linked list to move last node

Try it yourself

moving last node at first of linked list. that means some modification of link to connected node. the following process to do that.

1) first we need to find second last element of given linked list.


2) This second last node of linked list. that next node (Last node of linked list) next pointer value are NULL. Change this value and assign first node of linked list. Note that this process create cycle of linked list. Now change the value of root pointer to last node. And Assign NULL(No address) value to second node next pointer.

Finall step to move node

After move last node at first position.


Time complexity of this algorithm O(n).

Code Execution:

Code execution process

Not given all step execution process here.View How to insert linked list element, how to print and so on.

Try it yourself

c program to move last node at front of linked list.

Case 1: Empty Linked list 
  Empty linked List

 case 2: When linked list are not empty 
 Before move linked list :414  35  34  75  213  36  66  162  
 After move linked list :162  414  35  34  75  213  36  66  

 Free nodes of linked linked 

Spread the post

Recommended Posts: