π Introduction
The Rotate List problem is a classic linked list question frequently asked in coding interviews.
It tests your understanding of:
- linked list traversal
- pointer manipulation
- handling large constraints efficiently
π Try the problem yourself:
https://leetcode.com/problems/rotate-list/
π§ Intuition
Rotating a list means shifting elements to the right.
Example:
Instead of rotating one by one, we can:
- either simulate using extra space
- or optimize using pointer manipulation
π Problem Explanation
You are given:
- Head of a linked list
- Integer
k
Return the list after rotating it k times to the right
π§© Approach 1: Brute Force (Array-Based)
π‘ Idea
- Store values in array
- Rotate indices
- rebuild linked list
π» Java Code
π Dry Run (Brute Force)
Input:
Array:
After rotation logic:
Rebuild list β β Final Answer
β±οΈ Complexity
| Type | Value |
| Time | O(n) |
| Space | O(n) |
β οΈ Drawback
- Uses extra space
- Not optimal for large inputs
β‘ Approach 2: Optimal (Circular Linked List)
π‘ Idea
Instead of rebuilding:
- convert list into circular
- break at correct position
π» Java Code
π Dry Run (Optimal)
β±οΈ Complexity
| Type | Value |
| Time | O(n) |
| Space | O(1) |
π Comparison
| Approach | Time | Space | Best For |
| Array (Brute) | O(n) | O(n) | Beginners |
| Circular (Optimal) | O(n) | O(1) | Interviews |
π Related Problems
| Similar Pattern | Links |
| Reverse Linked List | Problem Link |
| Linked List Cycle | Problem Link |
| Remove Nth Node From End | Problem Link |
β οΈ Common Mistakes
- Forgetting
k % length - Incorrect pointer breaking
- Not handling single node
- Off-by-one in traversal
π― Key Points to Remember
- Always optimize k
- Use circular approach for best performance
- Understand pointer movement carefully
β FAQ
Q1: Why do we use k % length?
Because rotating more than length gives same result.
Q2: Which approach is better?
Optimal circular linked list approach.
Q3: Can we use array approach in interviews?
Yes, but optimal is preferred.
Q4: What is the key trick here?
Convert linked list into circular structure.




