π Rotating List by K Places


π§© The Problem:
Given a singly linked list, rotate it to the right by k
nodes.
Example: If the list is 1 β 2 β 3 β 4 β 5 β NULL
and k = 2
then,
rotate 1: 5 β 1 β 2 β 3 β 4 β NULL
rotate 2: 4 β 5 β 1 β 2 β 3 β NULL
the rotated list becomes( final output ):4 β 5 β 1 β 2 β 3 β NULL
π‘ Key Insight
For me, the key challenge was identifying the exact condition that ensures the rotation works correctly.
I realized that when
k == n
(total nodes), no rotation is needed.And if
k > n
, it loops back. So,k = k % n
simplifies the problem.
π οΈ Steps I Took:
Count the number of nodes
n
Update
k = k % n
to handle bigk
Make the list circular by connecting the last node to the head
Find the new tail β it's the
(n - k)
th nodeBreak the circle and update head
(n-k+1)th
node
π§ͺ The Code (C++)
void rotateNOdesByK(node *&head, int k)
{
if (!head || !head->next || k == 0) return head; // edge case(mandatory for leetCode)
node *temp = head;
int n = 1;
while (temp->next != nullptr)
{
temp = temp->next;
n++;
}
k = k % n; // to handle large k;
if (k == 0) return; // no rotation needed;
temp->next = head; // Step 3: Make it circular
temp = head;
for (int i = 1; i < (n - k); i++) // to reach at (n-k)th node;
{
temp = temp->next;
}
node *newHead = temp->next; // (n-k+1)th node
temp->next = nullptr; // (n-k)th node
head = newHead;
}
π LeetCode Tip : Avoid Runtime Errors on Edge Cases (must)
When solving linked list problems on platforms like LeetCode, donβt forget to handle these edge cases up front, or you'll face runtime errors.
if (!head || !head->next || k == 0) return head;
π§ Always remember:
βEdge cases are not extras, theyβre entry tests!β
π§ Key Takeaways:
Always do
k = k % n
when dealing with rotationsβitβs a lifesaver.Turning the list into a temporary circle makes the shifting logic easier.
Break down the problem into smaller stepsβit makes implementation clean and less buggy.
Time and Space Complexity( step-by-step ):
β Time Complexity: O(n)
Counting the number of nodes (
n
) βO(n)
Connecting the last node to the head β
O(1)
Traversing to the
(n - k)
th node βO(n)
in the worst case
Even though we have two separate traversals, theyβre both linear with respect to the size of the list, so:
Total Time = O(n)
β Space Complexity: O(1)
Not used any extra space (no array, no map, etc.), just a few pointers (temp
, newHead
) and counters.
Total Space = O(1)
Complexities:
Metric | Complexity |
Time Complexity | O(n) |
Space Complexity | O(1) |
Let me know your thoughts, and feel free to try rotating your own linked list with different values of k
βthe intuition gets better the more you visualize it!
π» Keep coding, π keep optimizing!
Subscribe to my newsletter
Read articles from Abhinav Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Abhinav Kumar
Abhinav Kumar
π B.Tech CSE | π¨βπ» Learning DSA & C++ | π Building projects & writing what I learn | π Currently solving LeetCode & exploring Git/GitHub