LinkedList/reverse_nodes_in_k_group library

� Reverse Nodes in K-Group — in-place group-wise reversal

Reverses nodes of a singly linked list in contiguous groups of size k. If the final group has fewer than k nodes, it remains in original order. The implementation reverses pointers in-place for O(1) additional space.

Contract:

  • Inputs: head (nullable), k (positive integer).
  • Output: new head of the transformed list (nullable). If k <= 1, returns original head.
  • Error modes: non-positive k will return the original list unchanged.

Complexity: Time O(n), Space O(1).

Example:

final head = LinkedListNode.fromList([1,2,3,4,5]);
final reversed = reverseNodesInKGroup(head, 2);
// reversed: [2,1,4,3,5]

Functions

reverseNodesInKGroup<T>(LinkedListNode<T>? head, int k) LinkedListNode<T>?