LinkedList/rotate_linked_list library
� Rotate Linked List — robust, generic implementation
Rotates a singly linked list to the right by k places and returns the new head.
The function operates in-place (O(1) extra space) and runs in a single pass to compute
length plus a second pass to rewire pointers when necessary.
Contract:
- Inputs:
head(nullable linked list head),k(non-negative rotation count). - Output: new head of the rotated list (nullable). Returns
nullwhenheadisnull. - Error modes: negative
kis treated as invalid (caller should provide non-negativek).
Complexity: Time O(n), Space O(1).
Example:
final head = LinkedListNode.fromList([1,2,3,4,5]);
final rotated = rotateLinkedList(head, 2);
// rotated: [4,5,1,2,3]
Functions
-
rotateLinkedList<
T> (LinkedListNode< T> ? head, int k) → LinkedListNode<T> ?