ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
vector<int> vals;
for (ListNode* n = list1; n; n = n->next) vals.push_back(n->val);
for (ListNode* n = list2; n; n = n->next) vals.push_back(n->val);
sort(vals.begin(), vals.end());
ListNode* dummy = new ListNode(0);
ListNode* tail = dummy;
for (int v : vals) {
tail->next = new ListNode(v);
tail = tail->next;
}
return dummy->next;
}
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
List<Integer> vals = new ArrayList<>();
for (ListNode n = list1; n != null; n = n.next) vals.add(n.val);
for (ListNode n = list2; n != null; n = n.next) vals.add(n.val);
Collections.sort(vals);
ListNode dummy = new ListNode(0);
ListNode tail = dummy;
for (int v : vals) {
tail.next = new ListNode(v);
tail = tail.next;
}
return dummy.next;
}
def merge_two_lists(list1, list2):
vals = []
n = list1
while n:
vals.append(n.val)
n = n.next
n = list2
while n:
vals.append(n.val)
n = n.next
vals.sort()
dummy = ListNode(0)
tail = dummy
for v in vals:
tail.next = ListNode(v)
tail = tail.next
return dummy.next
function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
const vals: number[] = [];
for (let n = list1; n; n = n.next) vals.push(n.val);
for (let n = list2; n; n = n.next) vals.push(n.val);
vals.sort((a, b) => a - b);
const dummy = new ListNode(0);
let tail = dummy;
for (const v of vals) {
tail.next = new ListNode(v);
tail = tail.next;
}
return dummy.next;
}
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
if (!list1) return list2;
if (!list2) return list1;
if (list1->val <= list2->val) {
list1->next = mergeTwoLists(list1->next, list2);
return list1;
}
list2->next = mergeTwoLists(list1, list2->next);
return list2;
}
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if (list1 == null) return list2;
if (list2 == null) return list1;
if (list1.val <= list2.val) {
list1.next = mergeTwoLists(list1.next, list2);
return list1;
}
list2.next = mergeTwoLists(list1, list2.next);
return list2;
}
def merge_two_lists(list1, list2):
if not list1:
return list2
if not list2:
return list1
if list1.val <= list2.val:
list1.next = merge_two_lists(list1.next, list2)
return list1
list2.next = merge_two_lists(list1, list2.next)
return list2
function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
if (!list1) return list2;
if (!list2) return list1;
if (list1.val <= list2.val) {
list1.next = mergeTwoLists(list1.next, list2);
return list1;
}
list2.next = mergeTwoLists(list1, list2.next);
return list2;
}
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
ListNode dummy(0);
ListNode* tail = &dummy;
ListNode* a = list1;
ListNode* b = list2;
while (a && b) {
if (a->val <= b->val) {
tail->next = a;
a = a->next;
} else {
tail->next = b;
b = b->next;
}
tail = tail->next;
}
tail->next = a ? a : b;
return dummy.next;
}
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode dummy = new ListNode(0);
ListNode tail = dummy;
ListNode a = list1;
ListNode b = list2;
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;
}
def merge_two_lists(list1, list2):
dummy = ListNode(0)
tail = dummy
a = list1
b = list2
while a and b:
if a.val <= b.val:
tail.next = a
a = a.next
else:
tail.next = b
b = b.next
tail = tail.next
tail.next = a or b
return dummy.next
function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
const dummy = new ListNode(0);
let tail = dummy;
let a = list1;
let b = list2;
while (a && b) {
if (a.val <= b.val) {
tail.next = a;
a = a.next;
} else {
tail.next = b;
b = b.next;
}
tail = tail.next;
}
tail.next = a ?? b;
return dummy.next;
}
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
ListNode dummy(0);
ListNode* tail = &dummy;
ListNode* a = list1;
ListNode* b = list2;
while (a && b) {
if (b->val < a->val) swap(a, b);
tail->next = a;
while (a->next && a->next->val <= b->val) a = a->next;
tail = a;
a = a->next;
}
tail->next = a ? a : b;
return dummy.next;
}
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode dummy = new ListNode(0);
ListNode tail = dummy;
ListNode a = list1;
ListNode b = list2;
while (a != null && b != null) {
if (b.val < a.val) { ListNode t = a; a = b; b = t; }
tail.next = a;
while (a.next != null && a.next.val <= b.val) a = a.next;
tail = a;
a = a.next;
}
tail.next = a != null ? a : b;
return dummy.next;
}
def merge_two_lists(list1, list2):
dummy = ListNode(0)
tail = dummy
a = list1
b = list2
while a and b:
if b.val < a.val:
a, b = b, a
tail.next = a
while a.next and a.next.val <= b.val:
a = a.next
tail = a
a = a.next
tail.next = a or b
return dummy.next
function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
const dummy = new ListNode(0);
let tail = dummy;
let a = list1;
let b = list2;
while (a && b) {
if (b.val < a.val) [a, b] = [b, a];
tail.next = a;
while (a.next && a.next.val <= b.val) a = a.next;
tail = a;
a = a.next;
}
tail.next = a ?? b;
return dummy.next;
}