How to Rotate Linked List with Easy 4 Steps,explained with images

By | February 21, 2017

Rotate the linked list by “k”th element’s in clockwise in a given linked list. let’s go by few examples

Example 1:

Input:- 10->20->30->40->50->60 and input k = 4

Output:- 50->60->10->20->30->40

Example 2:

Input:- 10->20->30->40->50->60 and input k = 3

Output:- 40->50->60->10->20->30

Rotate Linked List by Given Kth Element.

Rotate Linked List by Given Kth Element.

Here is the Main Logic to follow to solve the Problem ( follow from Step 1 to Step 4,that’s it 🙂

Step 1: Take our Input Linked list and Input Kth Element

Rotate Linked list Step 1

Rotate Linked list Step 1

Step 2:  Make, next of kth node to NULL.

Rotate Linked list Step 2

Rotate Linked list Step 2Step 3:  next of last node to previous head node.

Rotate Linked list Step 3

Rotate Linked list Step 3

Step 4:  change head to (k+1)th node.

Rotate Linked list Step 4

Rotate Linked list Step 4

Rotate Linked list Step 4

Rotate Linked list Step 4

#include <bits/stdc++.h>
#include <string.h>
using namespace std;
// Structure of node.It contains two elements.one is string.
// Second one is Pointer to Node

struct node
{
 int data;
 struct node* next;
};

//global variable
node *head;

/* Function to create a new node with given data */
node* newNode(int data)
{
 node *new_node = new node;
 new_node->data = data;
 new_node->next = NULL;
 return new_node;
}


void displayLinkedList(node *head)
{
 struct node *temp;
 
 if(head == NULL)
 {
 printf("I am sorry Bro,LinkedList is empty.\n\n");
 }
 else
 {
 temp = head;
 printf("Displaying linked List.\n\n");
 while(temp != NULL)
 {
 cout<<temp->data<<"=====>";
 temp = temp->next; 
 }
 
 printf("END\n\n"); 
 }
} 

// Main Logic 

void RotateLinkedList(node* head,int fromRotation)
{
 //fromRotation should be less than size of the linked list 
 if (fromRotation < 0 || fromRotation > 6)
 {
 return ;
 }
 
 //we need to move up to kth node in the linked list from current head.
 //lets take one temporary node.
 struct node * kth_node = head;
 struct node * LastNodeintheLinkedlist;
 struct node * stopKthNode;
 
 int counterforkthelement = 1;
 
 while (( kth_node!= NULL ) && (counterforkthelement < fromRotation))
 {
 kth_node = kth_node->next;
 counterforkthelement = counterforkthelement + 1;
 }
 //with the above loop,we reach to the kth Element in the linkedlist.
 
 if(kth_node == NULL)
 {
 return ;
 }
 
 //Now store kth_node into Variable "stopKthNode" node.
 stopKthNode = kth_node;
 
 
 //now,kth_node will point to last node of linked list.
 while (kth_node->next != NULL)
 {
 kth_node = kth_node->next;
 }
 
 
 //if you summarize above activitity,kth_node is pointer to last node.
 //stopKthNode is pointing to kth Element in the linkedlist.
 
 //As per given test data,60->10,
 kth_node->next = head;
 
 //As per given test data head -> 50
 head = stopKthNode->next;
 
 
 
 //As per given test data 40 -> NULL
 stopKthNode->next = NULL;
 
 printf("Existing Linked List Elements After Rotation of Kth Element - %d \n\n",fromRotation);
 
 //Display created linked list.
 displayLinkedList(head);
 
}

//Main program
int main()
{
 
 printf("Creating Linked list \n");
 /*Create head element */
 head = newNode(10);
 /*Add Second element 10->20 */
 head->next = newNode(20);
 /*Add Third element 10->20->30*/
 head->next->next = newNode(30);
 /*Add Fourth element 10->20->30->40*/
 head->next->next->next = newNode(40);
 /*Add Fourth element 10->20->30->40->50*/
 head->next->next->next->next = newNode(50);
 /*Add Fourth element 10->20->30->40->50->60*/
 head->next->next->next->next->next = newNode(60); 
 
 //Display created linked list.
 displayLinkedList(head);
 
 //Display created linked list.
 int fromRotation;
 printf("Enter the FROM to Rotate ( Example 1..2 ) \n");
 scanf("%d",&fromRotation);
 
 printf("Calling Rotation of Linkedlist Medhod for given - %d \n",fromRotation);
 RotateLinkedList(head,fromRotation);
 
 return 0;
}

Here, i have added screenshot on how to run the same program with output window.

Happy Simplylearning. 🙂

compile and run of complete program for rotation of linked list by kth element

compile and run of complete program for rotation of linked list by kth element

 

The following two tabs change content below.

SRINIVAS DARIPELLI

Myself SRINIVAS DARIPELLI. I have 15+ Years of Experience in Programming worked on multiple technologies.Apart from it,I am a blogger, writer, editor, artist and dad 🙂 .I believe in reality.I love to share the Helpful things around the Technology. Feel free to connect with me

One thought on “How to Rotate Linked List with Easy 4 Steps,explained with images

Leave a Reply