Rotate a Linked List

Rotate a Linked List

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.


If you want to learn more about the c language, here 10 Free days (up to 200 minutes) C video course for you.

Your free trial is waiting


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 the user to enter their choice to rotate the linked list.


Leave a Reply