πŸŒ€ Rotating List by K Places

Abhinav KumarAbhinav Kumar
3 min read

🧩 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:

  1. Count the number of nodes n

  2. Update k = k % n to handle big k

  3. Make the list circular by connecting the last node to the head

  4. Find the new tail β†’ it's the (n - k)th node

  5. Break 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)

  1. Counting the number of nodes (n) β€” O(n)

  2. Connecting the last node to the head β€” O(1)

  3. 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:

MetricComplexity
Time ComplexityO(n)
Space ComplexityO(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!

20
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