Iterative with dummy

Node merge(Node a, Node b) {
  Node dummy = new Node<>(0);
  Node tail = dummy;
  while (a != null && b != null) {
    if (a.val <= b.val) { tail.next = a; a = a.next; }
    else { tail.next = b; b = b.next; }
    tail = tail.next;
  }
  tail.next = (a != null) ? a : b;
  return dummy.next;
}
Advertisement

Recursive

Node merge(Node a, Node b) {
  if (a == null) return b;
  if (b == null) return a;
  if (a.val <= b.val) { a.next = merge(a.next, b); return a; }
  b.next = merge(a, b.next); return b;
}
Advertisement

In-place

Reuses existing nodes. No allocation except dummy head. O(1) auxiliary space.

Stability

Merge is stable if you use ≤ (not just <). Preserves relative order of equal elements.