LinkedList/swap_nodes_in_pairs library

� Swap Nodes in Pairs — safe adjacent node swapping

Performs an in-place swap of every two adjacent nodes in a singly linked list and returns the new head. The algorithm preserves node objects and only adjusts pointers (no value-swapping), making it suitable for nodes with complex payloads.

Contract:

  • Inputs: head (nullable linked list head).
  • Output: new head (nullable). If the list has odd length, the final node is left as-is.
  • Error modes: none; null input returns null.

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

Example:

final head = LinkedListNode.fromList([1,2,3,4]);
final swapped = swapNodesInPairs(head);
// swapped: [2,1,4,3]

Functions

swapNodesInPairs<T>(LinkedListNode<T>? head) LinkedListNode<T>?