Reverse Nodes in k-Group Problem

Reverse Nodes in k-Group Problem — ExecCode Hard DSA Practice

Solve the Reverse Nodes in k-Group problem on ExecCode. Free online hard DSA practice in Arrays - Basics. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Given the head of a linked list, reverse the nodes of the list k at a time, and return the modified list. Nodes not divisible by k remain as-is.

Examples

Input head = [1, 2, 3, 4, 5], k = 2; Output [2, 1, 4, 3, 5]

Constraints

1 ≤ k ≤ n ≤ 5000 0 ≤ Node.val ≤ 1000

Practice Reverse Nodes in k-Group free on ExecCode. Browse DSA problems, topic map, and placement guides.