ListNode merge(ListNode a, ListNode b) {
ListNode dummy = new ListNode(0);
ListNode tail = dummy;
while (a != null && b != null) {
if (a.value <= b.value) { 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;
}
def merge(a, b):
dummy = tail = ListNode(0)
while a and b:
if a.value <= b.value:
tail.next, a = a, a.next
else:
tail.next, b = b, b.next
tail = tail.next
tail.next = a or b
return dummy.next
def merge(a0: ListNode, b0: ListNode): ListNode =
var a = a0
var b = b0
val dummy = ListNode(0)
var tail = dummy
while a != null && b != null do
if a.value <= b.value then
tail.next = a
a = a.next
else
tail.next = b
b = b.next
tail = tail.next
tail.next = if a != null then a else b
dummy.next
ListNode* merge(ListNode* a, ListNode* b) {
ListNode dummy(0);
ListNode* tail = &dummy;
while (a != nullptr && b != nullptr) {
if (a->value <= b->value) {
tail->next = a;
a = a->next;
} else {
tail->next = b;
b = b->next;
}
tail = tail->next;
}
tail->next = a != nullptr ? a : b;
return dummy.next;
}