# Leetcode Rotate List problem solution In the Leetcode Rotate List problem solution Given the head of a linked list, rotate the list to the right by k places.

Example 1:

Input: head = [1,2,3,4,5], k = 2
Output: [4,5,1,2,3]

Example 2:

Input: head = [0,1,2], k = 4
Output: [2,0,1]

Constraints:

• The number of nodes in the list is in the range [0, 500].
• -100 <= Node.val <= 100
• 0 <= k <= 2 * 109

## Solution in C Programming

``````/**
* struct ListNode {
*     int val;
*     struct ListNode *next;
* };
*/

/* My Analysis
*  Time Complexity: O(n) + O(k) or O(n) + O(k*n)
*  Space Complexity: O(1)
*/
struct ListNode* rotateRight(struct ListNode* head, int k){

if ((pRetVal == NULL) || (pRetVal->next == NULL))
{
return pRetVal;
}

#if 1   // Time Complexity: O(n) + O(k)
while (pRetVal->next != NULL)
{
pRetVal = pRetVal->next;
}

while (k--)
{
}
pRetVal->next = NULL;
#else
#endif

}``````

## Solution in C++ Programming

``````
/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode() : val(0), next(nullptr) {}
*     ListNode(int x) : val(x), next(nullptr) {}
*     ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
}
int ct = 0;
ListNode *last = NULL;

while(temp != NULL){
ct++;
last = temp;
temp = temp->next;
}

if(k == ct){
}

if(k > ct){
k %= ct;
}

int num = ct - k;
int i = 1;
while(i < num){
i++;
temp = temp->next;
}

temp->next = NULL;

}
};``````

## Solution in Java Programming

``````class Solution {
public ListNode rotateRight(ListNode head, int k) {
ListNode pre = new ListNode(0);//pre ptr to return new list
//define a tail node, because the rotate operation will involve tailnode point head
ListNode tail = pre;
int len =0;
while(temp !=null){
len++;
temp = temp.next;
tail = tail.next;
}
//calculate the minimize rotate times
k = k % len;

//to find the cut point, then move all elements after the cut point to the head.
//cut point = move temp ptr (len- k) steps.

int i =1;
//redefine temp ptr point to pre
temp = pre;
while(i<=(len-k) & temp !=null){
temp = temp.next;
i++;
}
//if temp point to tail or it is  null, return the original list and end;
if(temp ==null || temp == tail) return pre.next;
//if not, move the pos elements to he head.
else{
pre.next = temp.next;
temp.next = null;
}
return pre.next;
}
}``````

## Solution in Python Programming

``````# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:

length = 1
# Get length
while node1 and node1.next:
node1 = node1.next
length += 1

k %= length
if not k:
i = 0
# Get the element before new head
while i != length - k - 1:
node2 = node2.next
i += 1

# Attach the last kth elements to the head to do the rotation
nextNode = node2.next
node2.next = None

return nextNode``````

## Solution in C# Programming

``````public ListNode RotateRight(ListNode head, int k) {

int count = 1;
while (curr.next != null)
{
curr = curr.next;
count++;
}

int c = 1, index = count - (k % count);
while (c != index)
{
curr = curr.next;
c++;
}

var result = curr.next;
curr.next = null;

return result;
}`````` #### By Neha Singhal

Hi, my name is Neha singhal a software engineer and coder by profession. I like to solve coding problems that give me the power to write posts for this site.