Rotate a Doubly Linked list in group of Given Size
Pattern: Doubly and circular lists
Walk the list in groups of k. Reverse each group by swapping its nodes' prev and next, then link it after the previous group in both directions. A shorter last group is reversed too.
The problem
Given the head of a doubly linked list and k, reverse the nodes in each group of k and return the new head. If the
last group has fewer than k nodes, reverse it as well. For example, 10 ⇄ 8 ⇄ 4 ⇄ 2 with k = 2 becomes
8 ⇄ 10 ⇄ 2 ⇄ 4, and 1 ⇄ 2 ⇄ 3 ⇄ 4 ⇄ 5 with k = 3 becomes 3 ⇄ 2 ⇄ 1 ⇄ 5 ⇄ 4.
The catalogue title says "rotate", but the linked GeeksforGeeks article reverses each group, and so does this guide.
The function name reverseInGroups and the rule that a short last group is reversed are this guide's own. In this
guide's code the node type is DNode, and the tests check every prev link.
Think first
- How do you reverse up to
knodes of a doubly linked list? - After reversing a group, which node is its first, and which its last?
- How do two reversed groups get joined?
Hints
Brute force, and why it fails
Copy values, reverse each block in an array, and write them back: O(n) memory and it changes values. Relinking in place needs O(1).
The key insight
Reversing a doubly linked group is the same pointer swap as reversing a whole list, stopped after k nodes. Only the
two joins between groups need extra care.
Dry run
The list is 10 ⇄ 8 ⇄ 4 ⇄ 2 and k = 2. The first group is 10 8.
The plan
newHead = null, prevGroupTail = null, cur = head
while cur is not null:
groupHead = cur, last = null
repeat up to k times while cur: next = cur.next; swap cur.prev, cur.next; last = cur; cur = next
if newHead is null: newHead = last
last.prev = prevGroupTail
if prevGroupTail: prevGroupTail.next = last
groupHead.next = null
prevGroupTail = groupHead
return newHead
Complexity
- Time
O(n)- Space
O(1)
Each node's pointers are swapped once and a few are fixed at the joins.
Edge cases
k = 1: unchanged.kat least the length: the whole list is reversed.- Empty list: empty.
I've built my logic, show the code
Java
class Solution {
public DNode reverseInGroups(DNode head, int k) {
DNode newHead = null, prevGroupTail = null, cur = head;
while (cur != null) {
DNode groupHead = cur, last = null;
for (int i = 0; i < k && cur != null; i++) { // reverse up to k nodes by swapping prev and next
DNode next = cur.next;
cur.next = cur.prev;
cur.prev = next;
last = cur;
cur = next;
}
// last is the group's new first node, groupHead its new last node
if (newHead == null) newHead = last;
last.prev = prevGroupTail;
if (prevGroupTail != null) prevGroupTail.next = last;
groupHead.next = null;
prevGroupTail = groupHead;
}
return newHead;
}
}C++
class Solution {
public:
DNode* reverseInGroups(DNode* head, int k) {
DNode *newHead = NULL, *prevGroupTail = NULL, *cur = head;
while (cur != NULL) {
DNode *groupHead = cur, *last = NULL;
for (int i = 0; i < k && cur != NULL; i++) { // reverse up to k nodes by swapping prev and next
DNode* next = cur->next;
cur->next = cur->prev;
cur->prev = next;
last = cur;
cur = next;
}
// last is the group's new first node, groupHead its new last node
if (newHead == NULL) newHead = last;
last->prev = prevGroupTail;
if (prevGroupTail != NULL) prevGroupTail->next = last;
groupHead->next = NULL;
prevGroupTail = groupHead;
}
return newHead;
}
};Python
from lib import DNode
class Solution:
def reverseInGroups(self, head: DNode, k: int) -> DNode:
new_head = prev_group_tail = None
cur = head
while cur is not None:
group_head, last = cur, None
for _ in range(k): # reverse up to k nodes by swapping prev and next
if cur is None:
break
nxt = cur.next
cur.next, cur.prev = cur.prev, nxt
last, cur = cur, nxt
# last is the group's new first node, group_head its new last node
if new_head is None:
new_head = last
last.prev = prev_group_tail
if prev_group_tail is not None:
prev_group_tail.next = last
group_head.next = None
prev_group_tail = group_head
return new_headCommon mistakes
- Leaving the short last group unreversed, which is a different rule (as in Reverse Nodes in k-Group).
- Forgetting
last.prev = prevGroupTail, which leaves a group pointing back inside itself. - Not clearing
groupHead.next, which keeps an old link into the next group.