Clockwise rotation of linked list

Given a linked list which include N nodes. And given a number K which indicates clockwise direction to rotate linked list node. Our goal is to rotate linked list nodes in clockwise.

 Example 1
 Input   :  1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
 Given k :  3
 Output  :  4 → 5 → 6 → 7 → 8 → 1 → 2 → 3 → NULL

 Example 2
 Input   :  4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
 Given k :  18
 Output  :  8 → 6 → -2 → 4 → 9 → 7 → 3 → NULL
 [
 	Note :
 	1) k is larger the number of linked list node.
 	First 7 rotation
 	4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL [7 rotation]
 	Second 7 rotation
 	4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL [14 rotation]
 	Next 4 rotation
 	8 → 6 → -2 → 4 → 9 → 7 → 3 → NULL [18 rotation]

 	Hint : use modulo operator to get final rotation
 ]

Here given code implementation process.

// C program for 
// Clockwise rotation of linked list 
#include <stdio.h>
#include <stdlib.h>

// Linked List LinkNode
struct LinkNode
{
	int data;
	struct LinkNode *next;
};
// Singly linked list 
struct SingleLL
{
	struct LinkNode *head;
	struct LinkNode *tail;
};
// Returns the new linked list
struct SingleLL *newLinkedList()
{
	// Create memory of head and tail Nodes
	struct SingleLL *sll = (struct SingleLL *) malloc(sizeof(struct SingleLL));
	if (sll == NULL)
	{
		printf("Memory overflow\n");
	}
	else
	{
		sll->head = NULL;
		sll->tail = NULL;
	}
	return sll;
}
// Add new Node at end of linked list 
void appendNode(struct SingleLL *sll, struct LinkNode *node)
{
	if (sll->head == NULL)
	{
		sll->head = node;
	}
	else
	{
		// Append the node at last position
		sll->tail->next = node;
	}
	sll->tail = node;
}
// Handles the request of adding new node in linked list
void addNode(struct SingleLL *sll, int data)
{
	// Create dynamic node
	struct LinkNode *node = (struct LinkNode *) malloc(sizeof(struct LinkNode));
	if (node == NULL)
	{
		printf("Memory overflow to Create LinkNode\n");
		return;
	}
	else
	{
		// Set initial node value
		node->data = data;
		node->next = NULL;
	}
	appendNode(sll, node);
}
// Display linked list element
void display(struct SingleLL *sll)
{
	if (sll->head == NULL)
	{
		return;
	}
	struct LinkNode *temp = sll->head;
	// iterating linked list elements
	while (temp != NULL)
	{
		printf(" %d →", temp->data);
		// Visit to next node
		temp = temp->next;
	}
	printf(" NULL\n");
}
// This are perform the linked list rotation
void rotation(struct SingleLL *sll, int k)
{
	// Define some auxiliary variable
	struct LinkNode *auxiliary = sll->head;
	int count = 0;
	// Count number of node in linked list
	while (auxiliary != NULL)
	{
		// visit to next node
		auxiliary = auxiliary->next;
		count++;
	}
	if (count == 1)
	{
		// When have only one node in linked list
		return;
	}
	// Get actual rotation
	count = k % count;
	if (count == 0)
	{
		// When actual linked list are not affected
		return;
	}
	auxiliary = sll->head;
	// Find the rotational node
	while (count > 1)
	{
		// visit to next node
		auxiliary = auxiliary->next;
		count--;
	}
	// Connecting the last node to first node of linked list
	sll->tail->next = sll->head;
	// Set new last node
	sll->tail = auxiliary;
	// Set new head
	sll->head = auxiliary->next;
	// Set that there is no node after of tail
	sll->tail->next = NULL;
}
// Handles the request to perform rotation
// k is indicate node rotation
void clockwiseRotation(struct SingleLL *sll, int k)
{
	if (k <= 0)
	{
		// When invalid given k
		return;
	}
	if (sll->head == NULL)
	{
		printf("\n Empty Linked List \n");
		return;
	}
	// Display given linked list
	printf("\n Before rotate  ");
	printf("\n Linked List  : ");
	display(sll);
	printf(" Given k : %d", k);
	// perform rotation
	rotation(sll, k);
	// Display resultant linked list
	printf("\n After rotate  ");
	printf("\n Linked List  : ");
	display(sll);
}
int main(int argc, char
	const *argv[])
{
	struct SingleLL *sll1 = newLinkedList();
	struct SingleLL *sll2 = newLinkedList();
	// First linked list
	//  1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
	addNode(sll1, 1);
	addNode(sll1, 2);
	addNode(sll1, 3);
	addNode(sll1, 4);
	addNode(sll1, 5);
	addNode(sll1, 6);
	addNode(sll1, 7);
	addNode(sll1, 8);
	// Second linked list
	//  4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
	addNode(sll2, 4);
	addNode(sll2, 9);
	addNode(sll2, 7);
	addNode(sll2, 3);
	addNode(sll2, 8);
	addNode(sll2, 6);
	addNode(sll2, -2);
	// Test case
	clockwiseRotation(sll1, 3);
	clockwiseRotation(sll2, 18);
	return 0;
}

input

 Before rotate
 Linked List  :  1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
 Given k : 3
 After rotate
 Linked List  :  4 → 5 → 6 → 7 → 8 → 1 → 2 → 3 → NULL

 Before rotate
 Linked List  :  4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
 Given k : 18
 After rotate
 Linked List  :  8 → 6 → -2 → 4 → 9 → 7 → 3 → NULL
/*
  Java Program for 
  Clockwise rotation of linked list 
*/
// Linked list node
class LinkNode
{
	public int data;
	public LinkNode next;
	public LinkNode(int data)
	{
		this.data = data;
		this.next = null;
	}
}
public class SingleLL
{
	public LinkNode head;
	public LinkNode tail;
	public SingleLL()
	{
		this.head = null;
		this.tail = null;
	}
	// Add new Node at end of linked list 
	public void addNode(int data)
	{
		LinkNode node = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	public void display()
	{
		if (this.head == null)
		{
			System.out.println("\n Empty linked list");
			return;
		}
		LinkNode temp = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			System.out.print(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		System.out.println(" NULL");
	}
	// This are perform the linked list rotation
	public void rotation(int k)
	{
		// Define some auxiliary variable
		LinkNode auxiliary = this.head;
		int count = 0;
		// Count number of node in linked list
		while (auxiliary != null)
		{
			// visit to next node
			auxiliary = auxiliary.next;
			count++;
		}
		if (count == 1)
		{
			// When have only one node in linked list
			return;
		}
		// Get actual rotation
		count = k % count;
		if (count == 0)
		{
			// When actual linked list are not affected
			return;
		}
		auxiliary = this.head;
		// Find the rotational node
		while (count > 1)
		{
			// visit to next node
			auxiliary = auxiliary.next;
			count--;
		}
		// Connecting the last node to first node of linked list
		this.tail.next = this.head;
		// Set new last node
		this.tail = auxiliary;
		// Set new head
		this.head = auxiliary.next;
		// Set that there is no node after of tail
		this.tail.next = null;
	}
	// Handles the request to perform rotation
	// k is indicate node rotation
	public void clockwiseRotation(int k)
	{
		if (k <= 0)
		{
			// When invalid given k
			return;
		}
		if (this.head == null)
		{
			System.out.print("\n Empty Linked List \n");
			return;
		}
		// Display given linked list
		System.out.println(" Before rotate ");
		System.out.println(" Linked List : ");
		display();
		System.out.println(" Given k : " + k);
		// perform rotation
		rotation(k);
		// Display resultant linked list
		System.out.println(" After rotate ");
		System.out.println(" Linked List : ");
		display();
      	System.out.print("\n");
	}
	public static void main(String[] args)
	{
		SingleLL sll1 = new SingleLL();
		SingleLL sll2 = new SingleLL();
		// First linked list
		//  1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
		sll1.addNode(1);
		sll1.addNode(2);
		sll1.addNode(3);
		sll1.addNode(4);
		sll1.addNode(5);
		sll1.addNode(6);
		sll1.addNode(7);
		sll1.addNode(8);
		// Second linked list
		//  4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
		sll2.addNode(4);
		sll2.addNode(9);
		sll2.addNode(7);
		sll2.addNode(3);
		sll2.addNode(8);
		sll2.addNode(6);
		sll2.addNode(-2);
		// Test case
		sll1.clockwiseRotation(3);
		sll2.clockwiseRotation(18);
	}
}

input

 Before rotate
 Linked List :
 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
 Given k : 3
 After rotate
 Linked List :
 4 → 5 → 6 → 7 → 8 → 1 → 2 → 3 → NULL

 Before rotate
 Linked List :
 4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
 Given k : 18
 After rotate
 Linked List :
 8 → 6 → -2 → 4 → 9 → 7 → 3 → NULL
// Include header file
#include <iostream>
using namespace std;

/*
  C++ Program for 
  Clockwise rotation of linked list 
*/

// Linked list node
class LinkNode
{
	public: int data;
	LinkNode *next;
	LinkNode(int data)
	{
		this->data = data;
		this->next = NULL;
	}
};
class SingleLL
{
	public: LinkNode *head;
	LinkNode *tail;
	SingleLL()
	{
		this->head = NULL;
		this->tail = NULL;
	}
	// Add new Node at end of linked list 
	void addNode(int data)
	{
		LinkNode *node = new LinkNode(data);
		if (this->head == NULL)
		{
			this->head = node;
		}
		else
		{
			// Append the node at last position
			this->tail->next = node;
		}
		this->tail = node;
	}
	// Display linked list element
	void display()
	{
		if (this->head == NULL)
		{
			cout << "\n Empty linked list" << endl;
			return;
		}
		LinkNode *temp = this->head;
		//iterating linked list elements
		while (temp != NULL)
		{
			cout << " " << temp->data << " →";
			// Visit to next node
			temp = temp->next;
		}
		cout << " NULL" << endl;
	}
	// This are perform the linked list rotation
	void rotation(int k)
	{
		// Define some auxiliary variable
		LinkNode *auxiliary = this->head;
		int count = 0;
		// Count number of node in linked list
		while (auxiliary != NULL)
		{
			// visit to next node
			auxiliary = auxiliary->next;
			count++;
		}
		if (count == 1)
		{
			// When have only one node in linked list
			return;
		}
		// Get actual rotation
		count = k % count;
		if (count == 0)
		{
			// When actual linked list are not affected
			return;
		}
		auxiliary = this->head;
		// Find the rotational node
		while (count > 1)
		{
			// visit to next node
			auxiliary = auxiliary->next;
			count--;
		}
		// Connecting the last node to first node of linked list
		this->tail->next = this->head;
		// Set new last node
		this->tail = auxiliary;
		// Set new head
		this->head = auxiliary->next;
		// Set that there is no node after of tail
		this->tail->next = NULL;
	}
	// Handles the request to perform rotation
	// k is indicate node rotation
	void clockwiseRotation(int k)
	{
		if (k <= 0)
		{
			// When invalid given k
			return;
		}
		if (this->head == NULL)
		{
			cout << "\n Empty Linked List \n";
			return;
		}
		// Display given linked list
		cout << " Before rotate " << endl;
		cout << " Linked List : " << endl;
		this->display();
		cout << " Given k : " << k << endl;
		// perform rotation
		this->rotation(k);
		// Display resultant linked list
		cout << " After rotate " << endl;
		cout << " Linked List : " << endl;
		this->display();
		cout << "\n";
	}
};
int main()
{
	SingleLL *sll1 = new SingleLL();
	SingleLL *sll2 = new SingleLL();
	// First linked list
	//  1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
	sll1->addNode(1);
	sll1->addNode(2);
	sll1->addNode(3);
	sll1->addNode(4);
	sll1->addNode(5);
	sll1->addNode(6);
	sll1->addNode(7);
	sll1->addNode(8);
	// Second linked list
	//  4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
	sll2->addNode(4);
	sll2->addNode(9);
	sll2->addNode(7);
	sll2->addNode(3);
	sll2->addNode(8);
	sll2->addNode(6);
	sll2->addNode(-2);
	// Test case
	sll1->clockwiseRotation(3);
	sll2->clockwiseRotation(18);
	return 0;
}

input

 Before rotate
 Linked List :
 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
 Given k : 3
 After rotate
 Linked List :
 4 → 5 → 6 → 7 → 8 → 1 → 2 → 3 → NULL

 Before rotate
 Linked List :
 4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
 Given k : 18
 After rotate
 Linked List :
 8 → 6 → -2 → 4 → 9 → 7 → 3 → NULL
// Include namespace system
using System;
/*
  Csharp Program for 
  Clockwise rotation of linked list 
*/
// Linked list node
public class LinkNode
{
	public int data;
	public LinkNode next;
	public LinkNode(int data)
	{
		this.data = data;
		this.next = null;
	}
}
public class SingleLL
{
	public LinkNode head;
	public LinkNode tail;
	public SingleLL()
	{
		this.head = null;
		this.tail = null;
	}
	// Add new Node at end of linked list 
	public void addNode(int data)
	{
		LinkNode node = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	public void display()
	{
		if (this.head == null)
		{
			Console.WriteLine("\n Empty linked list");
			return;
		}
		LinkNode temp = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			Console.Write(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		Console.WriteLine(" NULL");
	}
	// This are perform the linked list rotation
	public void rotation(int k)
	{
		// Define some auxiliary variable
		LinkNode auxiliary = this.head;
		int count = 0;
		// Count number of node in linked list
		while (auxiliary != null)
		{
			// visit to next node
			auxiliary = auxiliary.next;
			count++;
		}
		if (count == 1)
		{
			// When have only one node in linked list
			return;
		}
		// Get actual rotation
		count = k % count;
		if (count == 0)
		{
			// When actual linked list are not affected
			return;
		}
		auxiliary = this.head;
		// Find the rotational node
		while (count > 1)
		{
			// visit to next node
			auxiliary = auxiliary.next;
			count--;
		}
		// Connecting the last node to first node of linked list
		this.tail.next = this.head;
		// Set new last node
		this.tail = auxiliary;
		// Set new head
		this.head = auxiliary.next;
		// Set that there is no node after of tail
		this.tail.next = null;
	}
	// Handles the request to perform rotation
	// k is indicate node rotation
	public void clockwiseRotation(int k)
	{
		if (k <= 0)
		{
			// When invalid given k
			return;
		}
		if (this.head == null)
		{
			Console.Write("\n Empty Linked List \n");
			return;
		}
		// Display given linked list
		Console.WriteLine(" Before rotate ");
		Console.WriteLine(" Linked List : ");
		this.display();
		Console.WriteLine(" Given k : " + k);
		// perform rotation
		this.rotation(k);
		// Display resultant linked list
		Console.WriteLine(" After rotate ");
		Console.WriteLine(" Linked List : ");
		this.display();
		Console.Write("\n");
	}
	public static void Main(String[] args)
	{
		SingleLL sll1 = new SingleLL();
		SingleLL sll2 = new SingleLL();
		// First linked list
		//  1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
		sll1.addNode(1);
		sll1.addNode(2);
		sll1.addNode(3);
		sll1.addNode(4);
		sll1.addNode(5);
		sll1.addNode(6);
		sll1.addNode(7);
		sll1.addNode(8);
		// Second linked list
		//  4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
		sll2.addNode(4);
		sll2.addNode(9);
		sll2.addNode(7);
		sll2.addNode(3);
		sll2.addNode(8);
		sll2.addNode(6);
		sll2.addNode(-2);
		// Test case
		sll1.clockwiseRotation(3);
		sll2.clockwiseRotation(18);
	}
}

input

 Before rotate
 Linked List :
 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
 Given k : 3
 After rotate
 Linked List :
 4 → 5 → 6 → 7 → 8 → 1 → 2 → 3 → NULL

 Before rotate
 Linked List :
 4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
 Given k : 18
 After rotate
 Linked List :
 8 → 6 → -2 → 4 → 9 → 7 → 3 → NULL
<?php
/*
  Php Program for 
  Clockwise rotation of linked list 
*/
// Linked list node
class LinkNode
{
	public $data;
	public $next;
	public	function __construct($data)
	{
		$this->data = $data;
		$this->next = NULL;
	}
}
class SingleLL
{
	public $head;
	public $tail;
	public	function __construct()
	{
		$this->head = NULL;
		$this->tail = NULL;
	}
	// Add new Node at end of linked list 
	public	function addNode($data)
	{
		$node = new LinkNode($data);
		if ($this->head == NULL)
		{
			$this->head = $node;
		}
		else
		{
			// Append the node at last position
			$this->tail->next = $node;
		}
		$this->tail = $node;
	}
	// Display linked list element
	public	function display()
	{
		if ($this->head == NULL)
		{
			echo("\n Empty linked list".
				"\n");
			return;
		}
		$temp = $this->head;
		//iterating linked list elements
		while ($temp != NULL)
		{
			echo(" ".$temp->data.
				" →");
			// Visit to next node
			$temp = $temp->next;
		}
		echo(" NULL".
			"\n");
	}
	// This are perform the linked list rotation
	public	function rotation($k)
	{
		// Define some auxiliary variable
		$auxiliary = $this->head;
		$count = 0;
		// Count number of node in linked list
		while ($auxiliary != NULL)
		{
			// visit to next node
			$auxiliary = $auxiliary->next;
			$count++;
		}
		if ($count == 1)
		{
			// When have only one node in linked list
			return;
		}
		// Get actual rotation
		$count = $k % $count;
		if ($count == 0)
		{
			// When actual linked list are not affected
			return;
		}
		$auxiliary = $this->head;
		// Find the rotational node
		while ($count > 1)
		{
			// visit to next node
			$auxiliary = $auxiliary->next;
			$count--;
		}
		// Connecting the last node to first node of linked list
		$this->tail->next = $this->head;
		// Set new last node
		$this->tail = $auxiliary;
		// Set new head
		$this->head = $auxiliary->next;
		// Set that there is no node after of tail
		$this->tail->next = NULL;
	}
	// Handles the request to perform rotation
	// k is indicate node rotation
	public	function clockwiseRotation($k)
	{
		if ($k <= 0)
		{
			// When invalid given k
			return;
		}
		if ($this->head == NULL)
		{
			echo("\n Empty Linked List \n");
			return;
		}
		// Display given linked list
		echo(" Before rotate ".
			"\n");
		echo(" Linked List : ".
			"\n");
		$this->display();
		echo(" Given k : ".$k.
			"\n");
		// perform rotation
		$this->rotation($k);
		// Display resultant linked list
		echo(" After rotate ".
			"\n");
		echo(" Linked List : ".
			"\n");
		$this->display();
		echo("\n");
	}
}

function main()
{
	$sll1 = new SingleLL();
	$sll2 = new SingleLL();
	// First linked list
	//  1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
	$sll1->addNode(1);
	$sll1->addNode(2);
	$sll1->addNode(3);
	$sll1->addNode(4);
	$sll1->addNode(5);
	$sll1->addNode(6);
	$sll1->addNode(7);
	$sll1->addNode(8);
	// Second linked list
	//  4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
	$sll2->addNode(4);
	$sll2->addNode(9);
	$sll2->addNode(7);
	$sll2->addNode(3);
	$sll2->addNode(8);
	$sll2->addNode(6);
	$sll2->addNode(-2);
	// Test case
	$sll1->clockwiseRotation(3);
	$sll2->clockwiseRotation(18);
}
main();

input

 Before rotate
 Linked List :
 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
 Given k : 3
 After rotate
 Linked List :
 4 → 5 → 6 → 7 → 8 → 1 → 2 → 3 → NULL

 Before rotate
 Linked List :
 4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
 Given k : 18
 After rotate
 Linked List :
 8 → 6 → -2 → 4 → 9 → 7 → 3 → NULL
/*
  Node JS Program for 
  Clockwise rotation of linked list 
*/
// Linked list node
class LinkNode
{
	constructor(data)
	{
		this.data = data;
		this.next = null;
	}
}
class SingleLL
{
	constructor()
	{
		this.head = null;
		this.tail = null;
	}
	// Add new Node at end of linked list 
	addNode(data)
	{
		var node = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	display()
	{
		if (this.head == null)
		{
			console.log("\n Empty linked list");
			return;
		}
		var temp = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			process.stdout.write(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		console.log(" NULL");
	}
	// This are perform the linked list rotation
	rotation(k)
	{
		// Define some auxiliary variable
		var auxiliary = this.head;
		var count = 0;
		// Count number of node in linked list
		while (auxiliary != null)
		{
			// visit to next node
			auxiliary = auxiliary.next;
			count++;
		}
		if (count == 1)
		{
			// When have only one node in linked list
			return;
		}
		// Get actual rotation
		count = k % count;
		if (count == 0)
		{
			// When actual linked list are not affected
			return;
		}
		auxiliary = this.head;
		// Find the rotational node
		while (count > 1)
		{
			// visit to next node
			auxiliary = auxiliary.next;
			count--;
		}
		// Connecting the last node to first node of linked list
		this.tail.next = this.head;
		// Set new last node
		this.tail = auxiliary;
		// Set new head
		this.head = auxiliary.next;
		// Set that there is no node after of tail
		this.tail.next = null;
	}
	// Handles the request to perform rotation
	// k is indicate node rotation
	clockwiseRotation(k)
	{
		if (k <= 0)
		{
			// When invalid given k
			return;
		}
		if (this.head == null)
		{
			process.stdout.write("\n Empty Linked List \n");
			return;
		}
		// Display given linked list
		console.log(" Before rotate ");
		console.log(" Linked List : ");
		this.display();
		console.log(" Given k : " + k);
		// perform rotation
		this.rotation(k);
		// Display resultant linked list
		console.log(" After rotate ");
		console.log(" Linked List : ");
		this.display();
		process.stdout.write("\n");
	}
}

function main()
{
	var sll1 = new SingleLL();
	var sll2 = new SingleLL();
	// First linked list
	//  1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
	sll1.addNode(1);
	sll1.addNode(2);
	sll1.addNode(3);
	sll1.addNode(4);
	sll1.addNode(5);
	sll1.addNode(6);
	sll1.addNode(7);
	sll1.addNode(8);
	// Second linked list
	//  4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
	sll2.addNode(4);
	sll2.addNode(9);
	sll2.addNode(7);
	sll2.addNode(3);
	sll2.addNode(8);
	sll2.addNode(6);
	sll2.addNode(-2);
	// Test case
	sll1.clockwiseRotation(3);
	sll2.clockwiseRotation(18);
}
main();

input

 Before rotate
 Linked List :
 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
 Given k : 3
 After rotate
 Linked List :
 4 → 5 → 6 → 7 → 8 → 1 → 2 → 3 → NULL

 Before rotate
 Linked List :
 4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
 Given k : 18
 After rotate
 Linked List :
 8 → 6 → -2 → 4 → 9 → 7 → 3 → NULL
#  Python 3 Program for 
#  Clockwise rotation of linked list 

#  Linked list node
class LinkNode :
	def __init__(self, data) :
		self.data = data
		self.next = None
	

class SingleLL :
	def __init__(self) :
		self.head = None
		self.tail = None
	
	#  Add new Node at end of linked list 
	def addNode(self, data) :
		node = LinkNode(data)
		if (self.head == None) :
			self.head = node
		else :
			#  Append the node at last position
			self.tail.next = node
		
		self.tail = node
	
	#  Display linked list element
	def display(self) :
		if (self.head == None) :
			print("\n Empty linked list")
			return
		
		temp = self.head
		# iterating linked list elements
		while (temp != None) :
			print("", temp.data ,"→", end = "")
			#  Visit to next node
			temp = temp.next
		
		print(" NULL")
	
	#  This are perform the linked list rotation
	def rotation(self, k) :
		auxiliary = self.head
		count = 0
		#  Count number of node in linked list
		while (auxiliary != None) :
			#  visit to next node
			auxiliary = auxiliary.next
			count += 1
		
		if (count == 1) :
			#  When have only one node in linked list
			return
		
		#  Get actual rotation
		count = k % count
		if (count == 0) :
			#  When actual linked list are not affected
			return
		
		auxiliary = self.head
		#  Find the rotational node
		while (count > 1) :
			#  visit to next node
			auxiliary = auxiliary.next
			count -= 1
		
		#  Connecting the last node to first node of linked list
		self.tail.next = self.head
		#  Set new last node
		self.tail = auxiliary
		#  Set new head
		self.head = auxiliary.next
		#  Set that there is no node after of tail
		self.tail.next = None
	
	#  Handles the request to perform rotation
	#  k is indicate node rotation
	def clockwiseRotation(self, k) :
		if (k <= 0) :
			#  When invalid given k
			return
		
		if (self.head == None) :
			print("\n Empty Linked List ")
			return
		
		#  Display given linked list
		print(" Before rotate ")
		print(" Linked List : ")
		self.display()
		print(" Given k : ", k)
		#  perform rotation
		self.rotation(k)
		#  Display resultant linked list
		print(" After rotate ")
		print(" Linked List : ")
		self.display()
		print(end = "\n")
	

def main() :
	sll1 = SingleLL()
	sll2 = SingleLL()
	#  First linked list
	#   1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
	sll1.addNode(1)
	sll1.addNode(2)
	sll1.addNode(3)
	sll1.addNode(4)
	sll1.addNode(5)
	sll1.addNode(6)
	sll1.addNode(7)
	sll1.addNode(8)
	#  Second linked list
	#   4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
	sll2.addNode(4)
	sll2.addNode(9)
	sll2.addNode(7)
	sll2.addNode(3)
	sll2.addNode(8)
	sll2.addNode(6)
	sll2.addNode(-2)
	#  Test case
	sll1.clockwiseRotation(3)
	sll2.clockwiseRotation(18)

if __name__ == "__main__": main()

input

 Before rotate
 Linked List :
 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
 Given k :  3
 After rotate
 Linked List :
 4 → 5 → 6 → 7 → 8 → 1 → 2 → 3 → NULL

 Before rotate
 Linked List :
 4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
 Given k :  18
 After rotate
 Linked List :
 8 → 6 → -2 → 4 → 9 → 7 → 3 → NULL
#  Ruby Program for 
#  Clockwise rotation of linked list 

#  Linked list node
class LinkNode 
	# Define the accessor and reader of class LinkNode
	attr_reader :data, :next
	attr_accessor :data, :next
	def initialize(data) 
		self.data = data
		self.next = nil
	end

end

class SingleLL 
	# Define the accessor and reader of class SingleLL
	attr_reader :head, :tail
	attr_accessor :head, :tail
	def initialize() 
		self.head = nil
		self.tail = nil
	end

	#  Add new Node at end of linked list 
	def addNode(data) 
		node = LinkNode.new(data)
		if (self.head == nil) 
			self.head = node
		else 
			#  Append the node at last position
			self.tail.next = node
		end

		self.tail = node
	end

	#  Display linked list element
	def display() 
		if (self.head == nil) 
			print("\n Empty linked list", "\n")
			return
		end

		temp = self.head
		# iterating linked list elements
		while (temp != nil) 
			print(" ", temp.data ," →")
			#  Visit to next node
			temp = temp.next
		end

		print(" NULL", "\n")
	end

	#  This are perform the linked list rotation
	def rotation(k) 
		#  Define some auxiliary variable
		auxiliary = self.head
		count = 0
		#  Count number of node in linked list
		while (auxiliary != nil) 
			#  visit to next node
			auxiliary = auxiliary.next
			count += 1
		end

		if (count == 1) 
			#  When have only one node in linked list
			return
		end

		#  Get actual rotation
		count = k % count
		if (count == 0) 
			#  When actual linked list are not affected
			return
		end

		auxiliary = self.head
		#  Find the rotational node
		while (count > 1) 
			#  visit to next node
			auxiliary = auxiliary.next
			count -= 1
		end

		#  Connecting the last node to first node of linked list
		self.tail.next = self.head
		#  Set new last node
		self.tail = auxiliary
		#  Set new head
		self.head = auxiliary.next
		#  Set that there is no node after of tail
		self.tail.next = nil
	end

	#  Handles the request to perform rotation
	#  k is indicate node rotation
	def clockwiseRotation(k) 
		if (k <= 0) 
			#  When invalid given k
			return
		end

		if (self.head == nil) 
			print("\n Empty Linked List \n")
			return
		end

		#  Display given linked list
		print(" Before rotate ", "\n")
		print(" Linked List : ", "\n")
		self.display()
		print(" Given k : ", k, "\n")
		#  perform rotation
		self.rotation(k)
		#  Display resultant linked list
		print(" After rotate ", "\n")
		print(" Linked List : ", "\n")
		self.display()
		print("\n")
	end

end

def main() 
	sll1 = SingleLL.new()
	sll2 = SingleLL.new()
	#  First linked list
	#   1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
	sll1.addNode(1)
	sll1.addNode(2)
	sll1.addNode(3)
	sll1.addNode(4)
	sll1.addNode(5)
	sll1.addNode(6)
	sll1.addNode(7)
	sll1.addNode(8)
	#  Second linked list
	#   4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
	sll2.addNode(4)
	sll2.addNode(9)
	sll2.addNode(7)
	sll2.addNode(3)
	sll2.addNode(8)
	sll2.addNode(6)
	sll2.addNode(-2)
	#  Test case
	sll1.clockwiseRotation(3)
	sll2.clockwiseRotation(18)
end

main()

input

 Before rotate 
 Linked List : 
 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
 Given k : 3
 After rotate 
 Linked List : 
 4 → 5 → 6 → 7 → 8 → 1 → 2 → 3 → NULL

 Before rotate 
 Linked List : 
 4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
 Given k : 18
 After rotate 
 Linked List : 
 8 → 6 → -2 → 4 → 9 → 7 → 3 → NULL

/*
  Scala Program for 
  Clockwise rotation of linked list 
*/
// Linked list node
class LinkNode(var data: Int , var next: LinkNode)
{
	def this(data: Int)
	{
		this(data,null);
	}
}
class SingleLL(var head: LinkNode , var tail: LinkNode)
{
	def this()
	{
		this(null,null);
	}
	// Add new Node at end of linked list 
	def addNode(data: Int): Unit = {
		var node: LinkNode = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	def display(): Unit = {
		if (this.head == null)
		{
			println("\n Empty linked list");
			return;
		}
		var temp: LinkNode = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			print(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		println(" NULL");
	}
	// This are perform the linked list rotation
	def rotation(k: Int): Unit = {
		// Define some auxiliary variable
		var auxiliary: LinkNode = this.head;
		var count: Int = 0;
		// Count number of node in linked list
		while (auxiliary != null)
		{
			// visit to next node
			auxiliary = auxiliary.next;
			count += 1;
		}
		if (count == 1)
		{
			// When have only one node in linked list
			return;
		}
		// Get actual rotation
		count = k % count;
		if (count == 0)
		{
			// When actual linked list are not affected
			return;
		}
		auxiliary = this.head;
		// Find the rotational node
		while (count > 1)
		{
			// visit to next node
			auxiliary = auxiliary.next;
			count -= 1;
		}
		// Connecting the last node to first node of linked list
		this.tail.next = this.head;
		// Set new last node
		this.tail = auxiliary;
		// Set new head
		this.head = auxiliary.next;
		// Set that there is no node after of tail
		this.tail.next = null;
	}
	// Handles the request to perform rotation
	// k is indicate node rotation
	def clockwiseRotation(k: Int): Unit = {
		if (k <= 0)
		{
			// When invalid given k
			return;
		}
		if (this.head == null)
		{
			print("\n Empty Linked List \n");
			return;
		}
		// Display given linked list
		println(" Before rotate ");
		println(" Linked List : ");
		display();
		println(" Given k : " + k);
		// perform rotation
		rotation(k);
		// Display resultant linked list
		println(" After rotate ");
		println(" Linked List : ");
		display();
		print("\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var sll1: SingleLL = new SingleLL();
		var sll2: SingleLL = new SingleLL();
		// First linked list
		//  1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
		sll1.addNode(1);
		sll1.addNode(2);
		sll1.addNode(3);
		sll1.addNode(4);
		sll1.addNode(5);
		sll1.addNode(6);
		sll1.addNode(7);
		sll1.addNode(8);
		// Second linked list
		//  4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
		sll2.addNode(4);
		sll2.addNode(9);
		sll2.addNode(7);
		sll2.addNode(3);
		sll2.addNode(8);
		sll2.addNode(6);
		sll2.addNode(-2);
		// Test case
		sll1.clockwiseRotation(3);
		sll2.clockwiseRotation(18);
	}
}

input

 Before rotate
 Linked List :
 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
 Given k : 3
 After rotate
 Linked List :
 4 → 5 → 6 → 7 → 8 → 1 → 2 → 3 → NULL

 Before rotate
 Linked List :
 4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
 Given k : 18
 After rotate
 Linked List :
 8 → 6 → -2 → 4 → 9 → 7 → 3 → NULL
/*
  Swift 4 Program for 
  Clockwise rotation of linked list 
*/
// Linked list node
class LinkNode
{
	var data: Int;
	var next: LinkNode? ;
	init(_ data: Int)
	{
		self.data = data;
		self.next = nil;
	}
}
class SingleLL
{
	var head: LinkNode? ;
	var tail: LinkNode? ;
	init()
	{
		self.head = nil;
		self.tail = nil;
	}
	// Add new Node at end of linked list 
	func addNode(_ data: Int)
	{
		let node: LinkNode = LinkNode(data);
		if (self.head == nil)
		{
			self.head = node;
		}
		else
		{
			// Append the node at last position
			self.tail!.next = node;
		}
		self.tail = node;
	}
	// Display linked list element
	func display()
	{
		if (self.head == nil)
		{
			print("\n Empty linked list");
			return;
		}
		var temp: LinkNode? = self.head;
		//iterating linked list elements
		while (temp  != nil)
		{
			print("", temp!.data ,"→", terminator: "");
			// Visit to next node
			temp = temp!.next;
		}
		print(" NULL");
	}
	// This are perform the linked list rotation
	func rotation(_ k: Int)
	{
		// Define some auxiliary variable
		var auxiliary: LinkNode? = self.head;
		var count: Int = 0;
		// Count number of node in linked list
		while (auxiliary  != nil)
		{
			// visit to next node
			auxiliary = auxiliary!.next;
			count += 1;
		}
		if (count == 1)
		{
			// When have only one node in linked list
			return;
		}
		// Get actual rotation
		count = k % count;
		if (count == 0)
		{
			// When actual linked list are not affected
			return;
		}
		auxiliary = self.head;
		// Find the rotational node
		while (count > 1)
		{
			// visit to next node
			auxiliary = auxiliary!.next;
			count -= 1;
		}
		// Connecting the last node to first node of linked list
		self.tail!.next = self.head;
		// Set new last node
		self.tail = auxiliary;
		// Set new head
		self.head = auxiliary!.next;
		// Set that there is no node after of tail
		self.tail!.next = nil;
	}
	// Handles the request to perform rotation
	// k is indicate node rotation
	func clockwiseRotation(_ k: Int)
	{
		if (k <= 0)
		{
			// When invalid given k
			return;
		}
		if (self.head == nil)
		{
			print("\n Empty Linked List ");
			return;
		}
		// Display given linked list
		print(" Before rotate ");
		print(" Linked List : ");
		self.display();
		print(" Given k : ", k);
		// perform rotation
		self.rotation(k);
		// Display resultant linked list
		print(" After rotate ");
		print(" Linked List : ");
		self.display();
		print(terminator: "\n");
	}
}
func main()
{
	let sll1: SingleLL = SingleLL();
	let sll2: SingleLL = SingleLL();
	// First linked list
	//  1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
	sll1.addNode(1);
	sll1.addNode(2);
	sll1.addNode(3);
	sll1.addNode(4);
	sll1.addNode(5);
	sll1.addNode(6);
	sll1.addNode(7);
	sll1.addNode(8);
	// Second linked list
	//  4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
	sll2.addNode(4);
	sll2.addNode(9);
	sll2.addNode(7);
	sll2.addNode(3);
	sll2.addNode(8);
	sll2.addNode(6);
	sll2.addNode(-2);
	// Test case
	sll1.clockwiseRotation(3);
	sll2.clockwiseRotation(18);
}
main();

input

 Before rotate
 Linked List :
 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
 Given k :  3
 After rotate
 Linked List :
 4 → 5 → 6 → 7 → 8 → 1 → 2 → 3 → NULL

 Before rotate
 Linked List :
 4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
 Given k :  18
 After rotate
 Linked List :
 8 → 6 → -2 → 4 → 9 → 7 → 3 → NULL
/*
  Kotlin Program for 
  Clockwise rotation of linked list 
*/
// Linked list node
class LinkNode
{
	var data: Int;
	var next: LinkNode ? ;
	constructor(data: Int)
	{
		this.data = data;
		this.next = null;
	}
}
class SingleLL
{
	var head: LinkNode ? ;
	var tail: LinkNode ? ;
	constructor()
	{
		this.head = null;
		this.tail = null;
	}
	// Add new Node at end of linked list 
	fun addNode(data: Int): Unit
	{
		val node: LinkNode = LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail?.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	fun display(): Unit
	{
		if (this.head == null)
		{
			println("\n Empty linked list");
			return;
		}
		var temp: LinkNode ? = this.head;
		while (temp != null)
		{
			print(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		println(" NULL");
	}
	// This are perform the linked list rotation
	fun rotation(k: Int): Unit
	{
		// Define some auxiliary variable
		var auxiliary: LinkNode ? = this.head;
		var count: Int = 0;
		while (auxiliary != null)
		{
			// visit to next node
			auxiliary = auxiliary.next;
			count += 1;
		}
		if (count == 1)
		{
			// When have only one node in linked list
			return;
		}
		// Get actual rotation
		count = k % count;
		if (count == 0)
		{
			// When actual linked list are not affected
			return;
		}
		auxiliary = this.head;
		while (count > 1)
		{
			// visit to next node
			auxiliary = auxiliary?.next;
			count -= 1;
		}
		// Connecting the last node to first node of linked list
		this.tail?.next = this.head;
		// Set new last node
		this.tail = auxiliary;
		// Set new head
		this.head = auxiliary?.next;
		// Set that there is no node after of tail
		this.tail?.next = null;
	}
	// Handles the request to perform rotation
	// k is indicate node rotation
	fun clockwiseRotation(k: Int): Unit
	{
		if (k <= 0)
		{
			// When invalid given k
			return;
		}
		if (this.head == null)
		{
			print("\n Empty Linked List \n");
			return;
		}
		// Display given linked list
		println(" Before rotate ");
		println(" Linked List : ");
		this.display();
		println(" Given k : " + k);
		// perform rotation
		this.rotation(k);
		// Display resultant linked list
		println(" After rotate ");
		println(" Linked List : ");
		this.display();
		print("\n");
	}
}
fun main(args: Array < String > ): Unit
{
	val sll1: SingleLL = SingleLL();
	val sll2: SingleLL = SingleLL();
	// First linked list
	//  1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
	sll1.addNode(1);
	sll1.addNode(2);
	sll1.addNode(3);
	sll1.addNode(4);
	sll1.addNode(5);
	sll1.addNode(6);
	sll1.addNode(7);
	sll1.addNode(8);
	// Second linked list
	//  4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
	sll2.addNode(4);
	sll2.addNode(9);
	sll2.addNode(7);
	sll2.addNode(3);
	sll2.addNode(8);
	sll2.addNode(6);
	sll2.addNode(-2);
	// Test case
	sll1.clockwiseRotation(3);
	sll2.clockwiseRotation(18);
}

input

 Before rotate
 Linked List :
 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
 Given k : 3
 After rotate
 Linked List :
 4 → 5 → 6 → 7 → 8 → 1 → 2 → 3 → NULL

 Before rotate
 Linked List :
 4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
 Given k : 18
 After rotate
 Linked List :
 8 → 6 → -2 → 4 → 9 → 7 → 3 → NULL


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved