LeetCode 61 Rotate List Java Solution | Brute Force + Optimal Approach Explained (Linked List Rotation)
LeetCode 61 Rotate List Java Solution | Brute Force + Optimal Approach Explained (Linked List Rotation)

LeetCode 61 Rotate List Java Solution | Brute Force + Optimal Approach Explained (Linked List Rotation)

Learn how to solve LeetCode 61 Rotate List using brute force and optimized linked list approaches. Includes dry runs, edge cases, complexity analysis, and common mistakes.

7 views
0
0
Listen to articleAudio version

πŸš€ Introduction

The Rotate List problem is a classic linked list question frequently asked in coding interviews.

It tests your understanding of:

  1. linked list traversal
  2. pointer manipulation
  3. 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:

1 β†’ 2 β†’ 3 β†’ 4 β†’ 5, k = 2
β†’ 4 β†’ 5 β†’ 1 β†’ 2 β†’ 3

Instead of rotating one by one, we can:

  1. either simulate using extra space
  2. or optimize using pointer manipulation

πŸ“˜ Problem Explanation

You are given:

  1. Head of a linked list
  2. Integer k

Return the list after rotating it k times to the right

🧩 Approach 1: Brute Force (Array-Based)

πŸ’‘ Idea

  1. Store values in array
  2. Rotate indices
  3. rebuild linked list

πŸ’» Java Code

class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(head == null) return head;
int count = 0;
ListNode curr= head;
while(curr != null){
count++;
curr = curr.next;
}

curr = head;
int[] arr = new int[count];
for(int i =0; i <count;i++){
arr[i] = curr.val;
curr = curr.next;
}

ListNode ans = new ListNode(-1);
ListNode finAns = ans;

k = k % count;

for(int i = 0;i < arr.length;i++){
int ind =(i+(count-k))%arr.length;
ans.next = new ListNode(arr[ind]);
ans = ans.next;
}

return finAns.next;
}
}

πŸ” Dry Run (Brute Force)

Input:

[1,2,3,4,5], k = 2

Array:

[1,2,3,4,5]

After rotation logic:

[4,5,1,2,3]

Rebuild list β†’ βœ… Final Answer

⏱️ Complexity

TypeValue
TimeO(n)
SpaceO(n)

⚠️ Drawback

  1. Uses extra space
  2. Not optimal for large inputs

⚑ Approach 2: Optimal (Circular Linked List)

πŸ’‘ Idea

Instead of rebuilding:

  1. convert list into circular
  2. break at correct position

πŸ’» Java Code

class Solution {
public ListNode rotateRight(ListNode head, int k) {
if (head == null || head.next == null || k == 0) return head;

int length = 1;
ListNode curr = head;

while (curr.next != null) {
curr = curr.next;
length++;
}

curr.next = head;

k = k % length;
int steps = length - k;

ListNode newTail = curr;
while (steps-- > 0) {
newTail = newTail.next;
}

ListNode newHead = newTail.next;
newTail.next = null;

return newHead;
}
}

πŸ” Dry Run (Optimal)

Input: [1,2,3,4,5], k = 2

Length = 5
k = 2

Steps = 5 - 2 = 3

Move 3 steps β†’ new tail = 3
new head = 4

Final:
4 β†’ 5 β†’ 1 β†’ 2 β†’ 3

⏱️ Complexity

TypeValue
TimeO(n)
SpaceO(1)

πŸ“Š Comparison

ApproachTimeSpaceBest For
Array (Brute)O(n)O(n)Beginners
Circular (Optimal)O(n)O(1)Interviews

πŸ“š Related Problems

Similar PatternLinks
Reverse Linked ListProblem Link
Linked List CycleProblem Link
Remove Nth Node From EndProblem Link

⚠️ Common Mistakes

  1. Forgetting k % length
  2. Incorrect pointer breaking
  3. Not handling single node
  4. Off-by-one in traversal

🎯 Key Points to Remember

  1. Always optimize k
  2. Use circular approach for best performance
  3. 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.

Ai Assistant Kas