Given the head of a linked list, rotate the list to the right by k
places.
Example:
Given the linked list: 1->2->3->4->5->NULL
and k = 2
,
Return: 4->5->1->2->3->NULL
head == NULL
) or has only one node (head->next == NULL
), return the head as it is.k
is greater than the length of the list?
k % length
to rotate because rotating by the length of the list results in the same list.k
?
k
is always given as a non-negative integer.k % length
to determine the number of rotations needed.(length - k % length - 1)
and the new head will be the node immediately after the new tail.#include <iostream>
// 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 || !head->next || k == 0) return head;
// Calculate the length of the list
ListNode* current = head;
int length = 1; // start from 1 because we are already at head
while (current->next) {
current = current->next;
length++;
}
// Connect the last node to the head, forming a circular list
current->next = head;
// Find the point to break the circle at (new tail)
k = k % length;
int stepsToNewHead = length - k;
current = head;
for (int i = 1; i < stepsToNewHead; ++i) {
current = current->next;
}
// `current` is now pointing to the new tail
ListNode* newHead = current->next;
current->next = nullptr; // Break the circular list
return newHead;
}
};
// Helper function to print list (for debugging purposes)
void printList(ListNode* head) {
while (head) {
std::cout << head->val << " -> ";
head = head->next;
}
std::cout << "NULL" << std::endl;
}
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?