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
/**
* Definition for singly-linked list.
* 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){
struct ListNode* pRetVal = head;
if ((pRetVal == NULL) || (pRetVal->next == NULL))
{
return pRetVal;
}
int headSize = 0;
#if 1 // Time Complexity: O(n) + O(k)
while (pRetVal->next != NULL)
{
++headSize;
pRetVal = pRetVal->next;
}
k = headSize - (k % (headSize+1));
pRetVal->next = head;
while (k--)
{
head = head->next;
}
pRetVal = head;
head = head->next;
pRetVal->next = NULL;
#else
#endif
return head;
}
Solution in C++ Programming
/**
* Definition for singly-linked list.
* 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) {
if(head == NULL){
return head;
}
int ct = 0;
ListNode *temp = head;
ListNode *last = NULL;
while(temp != NULL){
ct++;
last = temp;
temp = temp->next;
}
if(k == ct){
return head;
}
if(k > ct){
k %= ct;
}
temp = head;
int num = ct - k;
int i = 1;
while(i < num){
i++;
temp = temp->next;
}
last->next = head;
head = temp->next;
temp->next = NULL;
return head;
}
};
Solution in Java Programming
class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(head ==null || head.next ==null) return head;
ListNode pre = new ListNode(0);//pre ptr to return new list
pre.next = head;
ListNode temp = head;
//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;
tail.next = head;
}
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:
def rotateRight(self, head, k):
if not head or not head.next or not k:
return head
node1 = head
length = 1
# Get length
while node1 and node1.next:
node1 = node1.next
length += 1
k %= length
if not k:
return head
i = 0
node2 = head
# 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
node1.next = head
return nextNode
Solution in C# Programming
public ListNode RotateRight(ListNode head, int k) {
if (head == null || head.next == null)
return head;
var curr = head;
int count = 1;
while (curr.next != null)
{
curr = curr.next;
count++;
}
curr.next = head;
int c = 1, index = count - (k % count);
curr = head;
while (c != index)
{
curr = curr.next;
c++;
}
var result = curr.next;
curr.next = null;
return result;
}
Also read,