Definition
A variation of reverse-in-k-groups where reversal and skipping alternate. For each cycle, reverse one group of k nodes and re-stitch it, then walk forward another k nodes without reversing, then repeat. A trailing group shorter than k in a reverse turn is left unchanged.
The National Bank of India prints slips where every other bundle of k is flipped and the bundles in between are left as-is, giving a striped pattern. The clerk flips the first bundle, leaves the next bundle alone, flips the next, and so on down the chain.
Understanding the Concepts
What is the problem?
Reverse alternating K-element Sub-list (Reverse Every Other Group of K). Given the head of a singly linked list and a positive integer k, reverse every alternate group of k nodes in place: reverse the first k, skip the next k, reverse the following k, and so on. Leave any final partial group as is. Use O(1) extra space.
How does the approach work?
Carry a previousTail just like the k-group version, but after each reversed group, advance it forward k nodes over the skipped group before the next reversal. The reversal and re-stitch are identical to the k-group problem. The only addition is the skip loop: after re-linking a reversed group, step previousTail forward k times (or until null) across the group that must stay in order, so the next reversal begins at the correct place.
For example with 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 and k=2, the result is 2 -> 1 -> 3 -> 4 -> 6 -> 5 -> 7 -> 8: the first and third pairs are flipped, the second and fourth are untouched. The skip must still advance even when the skipped group is short, which simply lands previousTail at null and ends the loop.
A senior-level nuance is that the alternating structure makes the boundary tracking slightly trickier, because previousTail must traverse the skipped nodes to stay positioned, but the reversal machinery is unchanged. Recognizing that the only new piece is the forward skip is what makes this feel easy once the k-group version is understood.
What is the banking use case?
The code contains a RegisterNode, a StripedRegister with reverseAlternate(head, k), and a demo.
How do you implement this in Java?
// ─── File: src/main/java/com/bank/striped/RegisterNode.java ───
package com.bank.striped;
public class RegisterNode {
final int id;
RegisterNode next;
RegisterNode(int id) { this.id = id; }
}
// ─── File: src/main/java/com/bank/striped/StripedRegister.java ───
package com.bank.striped;
public class StripedRegister {
public RegisterNode reverseAlternate(RegisterNode head, int k) {
if (head == null || k < 2) return head;
RegisterNode dummy = new RegisterNode(-1);
dummy.next = head;
RegisterNode previousTail = dummy;
while (previousTail != null && previousTail.next != null) {
// Ensure a full group of k remains to reverse.
RegisterNode check = previousTail;
for (int i = 0; i < k && check != null; i++) check = check.next;
if (check == null) break;
RegisterNode groupFirst = previousTail.next; // becomes group tail
RegisterNode previous = null, current = groupFirst;
for (int i = 0; i < k; i++) { // reverse k nodes
RegisterNode next = current.next;
current.next = previous;
previous = current;
current = next;
}
previousTail.next = previous; // link reversed group
groupFirst.next = current;
previousTail = groupFirst; // now at tail of reversed group
// SKIP the next k nodes (leave them in order).
for (int i = 0; i < k && previousTail != null; i++) previousTail = previousTail.next;
}
return dummy.next;
}
}
// ─── File: src/main/java/com/bank/striped/AlternateDemo.java ───
package com.bank.striped;
import java.util.logging.Logger;
public class AlternateDemo {
private static final Logger log = Logger.getLogger(AlternateDemo.class.getName());
public static void main(String[] args) {
RegisterNode head = build(new int[]{1, 2, 3, 4, 5, 6, 7, 8});
log.info("Original: " + render(head));
RegisterNode out = new StripedRegister().reverseAlternate(head, 2);
log.info("Alternating reverse k=2: " + render(out));
}
static RegisterNode build(int[] ids) {
RegisterNode d = new RegisterNode(-1), t = d;
for (int id : ids) { t.next = new RegisterNode(id); t = t.next; }
return d.next;
}
static String render(RegisterNode n) {
StringBuilder sb = new StringBuilder();
while (n != null) { sb.append(n.id);
if (n.next != null) sb.append(" -> "); n = n.next; }
return sb.append(" -> null").toString();
}
}
What is the project structure?
striped-register/
├── pom.xml
└── src/main/java/com/bank/striped/
├── AlternateDemo.java
├── RegisterNode.java
└── StripedRegister.java
How do you build and run?
# Build
mvn clean package -DskipTests
# Run
java -cp target/classes com.bank.striped.AlternateDemo
What is the step-by-step walkthrough?
Input data: chain 1..8, k=2.
previousTail is dummy. Nodes 1,2 form a full group; reverse to 2 -> 1. Re-stitch so dummy.next is node 2 and node 1's next is node 3. previousTail is node 1.previousTail to node 4. These stay in order.6 -> 5. Re-stitch so node 4's next is node 6 and node 5's next is node 7. previousTail is node 5.previousTail to node 8 and then its next is null. These stay in order. The loop ends.2 -> 1 -> 3 -> 4 -> 6 -> 5 -> 7 -> 8.Final output:
2026-10-27 19:25:42 [INFO] Original: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> null
2026-10-27 19:25:42 [INFO] Alternating reverse k=2: 2 -> 1 -> 3 -> 4 -> 6 -> 5 -> 7 -> 8 -> null
What is the high-level design?
═══ HLD — Striped Settlement Register ═══
┌──────────┐ build ┌────────────────────┐ render ┌──────────────┐
│ Ops UI │──────→│ Register Svc ×2 │───────→│ report chain │
└──────────┘ │ reverseAlternate(k)│ └──────────────┘
└────────────────────┘
What is the low-level design?
═══ LLD — StripedRegister ═══
┌─ Only new piece vs k-group ─────────────────────────┐
│ after each reversed group: advance previousTail k │
│ forward across the skipped group │
└──────────────────────────────────────────────────────┘
┌──────────────────────────────────────────┐
│ StripedRegister │
│ + reverseAlternate(head,k):RegisterNode │ O(n), O(1)
└──────────────────────────────────────────┘
Contract for reverseAlternate(head,k):
Pre: k ≥ 2
Post: odd-indexed groups reversed, even-indexed intact
What is the time and space complexity?
Analyze the solution based on the algorithm used. Consider both the time to process input and the space required for any data structures.
What are common mistakes?
Common mistakes include off-by-one errors, not handling edge cases like empty input, and not considering the constraints of the problem.
What are the edge cases?
Edge cases include empty input, single element input, all identical elements, and boundary values.
What should a senior engineer know?
A senior engineer should understand the time-space tradeoffs, be able to explain the algorithm clearly, and know when this technique applies versus alternatives.
Visual Overview
Input: 1..8 (k=2)
│
▼
StripedRegister.reverseAlternate()
│ reverse [1,2] → 2 -> 1
│ skip [3,4]
│ reverse [5,6] → 6 -> 5
│ skip [7,8]
▼
Output: 2 -> 1 -> 3 -> 4 -> 6 -> 5 -> 7 -> 8
Key Takeaway
Reversing alternate groups of k is the reverse-in-k-groups problem with one extra move per cycle. I keep a previous-tail pointer starting at a dummy, and in each cycle I first confirm a full group of k remains, reverse that group with the standard three-pointer technique, and re-stitch it exactly as in the k-group problem so the previous tail points to the group's new head and the group's original first node points to the next group's start. The one addition is that after re-linking a reversed group, I advance the previous tail forward k nodes across the next group without reversing it, which produces the striped alternating pattern.
The reversal machinery is completely unchanged; the only subtlety is that the previous tail must physically walk across the skipped nodes to stay correctly positioned for the next reversal, and when the skip runs off the end the loop simply terminates. Everything stays a single forward pass in constant space.
In my work at the National Bank of India this generated a striped settlement register, where every alternate bundle of k slips was shown reversed to visually separate reconciled bundles from pending ones while the in-between bundles kept their natural order. Flipping one bundle, skipping the next, and repeating in place gave that striped layout without any extra allocation, and I explain to an interviewer that once you have the k-group version, this is just that plus a forward skip.
Remember one sentence: understand the core algorithm and apply it to banking domain problems.
---