Back to all solutions
#25 - Reverse Nodes in k-Group
Problem Description
Given the head of a linked list, reverse the nodes of the list k at a time, and return the modified list.
k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is.
You may not alter the values in the list's nodes, only nodes themselves may be changed.
Solution
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @param {number} k
* @return {ListNode}
*/
var reverseKGroup = function(head, k) {
const result = new ListNode(null, head);
const stack = [];
let tail = result;
while (head) {
for (let i = 0; i < k && head; i++) {
stack.push(head);
head = head.next;
}
if (stack.length === k) {
while (stack.length) {
tail.next = stack.pop();
tail = tail.next;
}
tail.next = head;
}
}
return result.next;
};