700013. Linked List Group Reverse
Reverse a Linked List in Groups of Given Size
Slug: reverse-a-linked-list-in-groups-of-given-size
Difficulty: Hard
Topic Tags: Linked List, Data Structures
Company Tags: Paytm, VMWare, Accolite, Amazon, Microsoft, Snapdeal, Hike, MakeMyTrip, Walmart, Goldman Sachs, Adobe, SAP Labs
Summary
Given a linked list and an integer k, reverse the linked list in groups of size k. If the length of the linked list is not divisible by k, the remaining elements should be reversed as well.
This problem involves manipulating a linked list using recursion or iteration, while also considering the size of the groups to be reversed.
Detailed Explanation
The algorithm for this problem can be implemented using either recursion or iteration. We will use an iterative approach for simplicity and clarity.
Here's a step-by-step breakdown of the solution:
- Initialize three pointers:
prev,curr, andnext. SetprevtoNULLandcurrto the head of the linked list. - Loop through the linked list in groups of size k:
- Set
countto 0. This variable will keep track of the number of nodes in each group. - Loop until
countis less than or equal to k:- Move the
currpointer to the next node in the linked list (curr = curr->next;). - Increment
count.
- Move the
- When
countis greater than or equal to k, reverse the current group of nodes:- Set a temporary pointer
temptoprev. This will keep track of the previous node. - Move the
currpointer back to the start of the group (curr = prev->next;). - Loop until the end of the group is reached:
- Set the next pointer of each node in the group to the previous node (
temp->next = curr;). - Move the
tempandcurrpointers one step forward.
- Set the next pointer of each node in the group to the previous node (
- Reset
countto 0.
- Set a temporary pointer
- If there are remaining nodes that were not part of a complete group (i.e.,
count < k), reverse these nodes as well:- Set
prevto the last node in the group (prev = curr;). - Move the
currpointer to the next node in the linked list (curr = curr->next;). - Loop until the end of the remaining nodes is reached:
- Set the next pointer of each node to the previous node (
prev->next = curr;). - Move the
prevandcurrpointers one step forward.
- Set the next pointer of each node to the previous node (
- Set
- Set
- Return the modified linked list.
The time complexity for this algorithm is O(n), where n is the length of the linked list, since we are traversing the linked list once. The space complexity is O(1), since we are using a constant amount of extra space to store the temporary pointer and other variables.
Optimized Solutions
Java
public class Solution {
public ListNode reverseBetween(ListNode head, int k) {
if (head == null || k < 1) {
return head;
}
int count = 0;
ListNode prev = null;
ListNode curr = head;
while (count < k - 1) {
prev = curr;
curr = curr.next;
count++;
}
ListNode start = curr;
ListNode end = curr;
while (end.next != null) {
end = end.next;
}
while (curr != end) {
ListNode nextNode = curr.next;
curr.next = prev;
prev = curr;
curr = nextNode;
}
start.next = end;
return head;
}
}