public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null && l2 == null) return null;
if (l1 == null || l2 == null) {
if (l1 == null) return l2;
if (l2 == null) return l1;
}
ListNode head = new ListNode(-1);
ListNode end = head;
while (l1 != null && l2 != null) {
ListNode node;
if (l1.val < l2.val) {
node = new ListNode(l1.val);
l1 = l1.next;
end.next = node;
end = node;
}
else if (l1.val == l2.val){
node = new ListNode(l1.val);
ListNode node1 = new ListNode(l2.val);
l1 = l1.next;
l2 = l2.next;
end.next = node;
end = node;
end.next = node1;
end = node1;
}
else {
node = new ListNode(l2.val);
end.next = node;
end = node;
l2 = l2.next;
}
}
while (l1 != null) {
ListNode node = new ListNode(l1.val);
end.next = node;
end = node;
l1 = l1.next;
}
while (l2 != null) {
ListNode node = new ListNode(l2.val);
l2 = l2.next;
end.next = node;
end = node;
}
return head.next;
}
04-16
440
06-23
543
01-01
2334