In this article, I will discuss how to rotate a linked list by its n node. That means if an existing linked list is 1->2->3->4->5 and a user entered 2 then linked list should be modified to 3->4->5->1->2.
Note: The value of n should be less than the length of the linked-list.



Steps to rotate a single linked list

  • Traverse the linked-list by n nodes.
  • Keep nth node in TmpNode.
  • After that traverse, traverse the linked-list till the end and assign the address of the first node to the last node.
  • Assign address of (n+1)node to the head (start) pointer.
  • Point nth node next to NULL.




Code to rotate linked-list

This code rotates the linked list as per the user choice.




Driver program to rotate the linked list

In this driver source code user create a linked list as per the choice. After the creation of the linked list, it asked from the user to enter their choice to rotate the linked list.

OutPut: