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

Spread the post

Move first node at end of given linked list

The following process to move the first node at end of given linked list

1) initial root pointer is pointing to first node of linked list.

2) help of root pointer visit all other linked list node. and find last node of linked list.

3) Find this last node of linked list assign this address to new pointer variable like(temp variable).And modifying temp->next==NULL to temp->next=root. that means last node of linked list next pointer are initial null that are modified this value to first node of linked list. this is cycle condition there will be removing on step 5.

temp->next->next=root;

4) root node pointer value will be modified to next node of linked list.

root=root->next;

5) and modifying first node next pointer value is null. note that temp variable are pointed to last of linked list there next node are first node of linked list.

temp->next->next=NULL;

Example:

Suppose following data [505, 545, 449, 554, 584, 497, 525, 520, 424, 556] are inserted on linked list.

Given linked list to move first node

Try it yourself

After move node

After move first node

Algorithm:

Time complexity of this algorithm O(n).

Code Execution:

Code execution process

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

Try it yourself

c program to move the first node at end of given linked list. Iterative approach.

Output
 Case 1: Empty Linked list 
  Empty linked List

 case 2: When linked list are not empty 
 
 Before move linked list :550  424  520  525  497  584  554  449  545  505  
 After move linked list :424  520  525  497  584  554  449  545  505  550  

 Free nodes of linked linked 

Spread the post

Recommended Posts: