# Rotate linked list clockwise by k nodes in php

Php program for Rotate linked list clockwise by k nodes. Here mentioned other language solution.

``````<?php
/*
Php program for
Rotate a list by moving each element
k times to the right.
Or clockwise rotation of linked list
*/
{
public \$data;
public \$next;
public	function __construct(\$data)
{
\$this->data = \$data;
\$this->next = NULL;
}
}
class SingleLL
{
public \$tail;
public	function __construct()
{
\$this->tail = NULL;
}
{
{
}
else
{
// Append the node at last position
\$this->tail->next = \$node;
}
\$this->tail = \$node;
}
public	function display()
{
{
return;
}
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
\$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 less than 2 nodes in linked list
return;
}
// Get actual rotation
\$count = \$k % \$count;
if (\$count == 0)
{
// When actual linked list are not affected
return;
}
// 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
// Set new last node
\$this->tail = \$auxiliary;
// 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;
}
{
return;
}
echo " Before rotate \n";
echo " Linked List : \n";
\$this->display();
echo " Given k : \$k \n";
// Perform rotation
\$this->rotation(\$k);
echo " After rotate \n";
echo " Linked List : \n";
\$this->display();
echo "\n\n";
}
public static function main()
{
\$sll1 = new SingleLL();
\$sll2 = new SingleLL();
// 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
// 4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
// Test case
\$sll1->clockwiseRotation(3);
\$sll2->clockwiseRotation(18);
}
}
SingleLL::main();``````

Output

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

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