java解答
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1 == null && l2 == null) {
return null;
}
if(l1 == null) {
return l2;
}
if(l2 == null) {
return l1;
}
ListNode n1 = l1, n2 = l2;
ListNode head = new ListNode(-1);
ListNode curTail = head;
while(n1 != null && n2 != null) {
if(n1.val < n2.val) {
curTail.next = n1;
n1 = n1.next;
} else {
curTail.next = n2;
n2 = n2.next;
}
curTail = curTail.next;
}
if(n1 != null) {
curTail.next = n1;
}
if(n2 != null) {
curTail.next = n2;
}
return head.next;
}
}