Reversing a Singly Linked List in K‑Node Groups Starting from the Tail
The article explains how to solve a variant of the linked‑list reversal problem where every K nodes are reversed as a group starting from the tail, using recursion and double reversal techniques, and provides complete Java implementations for the algorithm.
A friend recently interviewed at ByteDance and was asked a linked‑list algorithm question that he could not solve, prompting this detailed walkthrough.
Problem statement: Given the head of a singly linked list, implement a function that reverses every K nodes as a group, but the grouping starts from the tail of the list; any remaining nodes at the head that do not form a full group are left unchanged. No auxiliary queue or stack may be used.
Example: For the list 1→2→3→4→5→6→7→8→null with K = 3, the groups from the tail are (6→7→8), (3→4→5), and (1→2). After processing the list becomes 1→2→5→4→3→8→7→6→null, while the first two nodes remain unchanged because they do not form a complete group.
The main difficulty is that the grouping starts from the tail, which is not straightforward for a singly linked list. Recursion is a natural fit for this scenario; readers unfamiliar with recursion are encouraged to consult the linked articles on recursion techniques.
Before tackling the tail‑grouping version, the article reviews the simpler variant where grouping starts from the head, showing how the same list would be transformed to 3→2→1→6→5→4→7→8→null (the last two nodes stay as they are).
Two helper methods are presented:
// Reverse each K‑node group (starting from the head)
public ListNode reverseKGroup(ListNode head, int k) {
ListNode temp = head;
for (int i = 1; i < k && temp != null; i++) {
temp = temp.next;
}
// If there are fewer than k nodes, return the original head
if (temp == null) return head;
ListNode t2 = temp.next;
temp.next = null;
// Reverse the current group
ListNode newHead = reverseList(head);
// Recursively process the remaining list
ListNode newTemp = reverseKGroup(t2, k);
// Connect the two parts
head.next = newTemp;
return newHead;
}
// Reverse an entire singly linked list
private static ListNode reverseList(ListNode head) {
if (head == null || head.next == null) return head;
ListNode result = reverseList(head.next);
head.next.next = head;
head.next = null;
return result;
}To solve the original tail‑grouping problem, the article proposes a three‑step approach:
Reverse the whole list once.
Apply the reverseKGroup method (which assumes grouping from the head).
Reverse the list a second time to restore the original orientation.
The combined solution is implemented as follows:
public ListNode solve(ListNode head, int k) {
// First reversal
head = reverse(head);
// Group‑wise reversal starting from the (now) head
head = reverseKGroup(head, k);
// Final reversal to obtain the correct order
head = reverse(head);
return head;
}The article also mentions a related interview question that requires reversing two linked lists, adding their corresponding nodes, and then merging the result, illustrating the versatility of the double‑reversal technique.
In summary, the piece highlights that linked‑list problems are common in technical interviews and encourages readers to practice similar algorithmic challenges.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.