// 由于都是有序链表,双指针,哪个小就取哪个
// 注意输入有空的情况
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode res = null;
if (l1 == null) return l2;
if (l2 == null) return l1;
if (l1.val < l2.val) {
res = l1;
l1 = l1.next;
} else {
res = l2;
l2 = l2.next;
}
ListNode head = res;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
res.next = l1;
l1 = l1.next;
res = res.next;
} else {
res.next = l2;
l2 = l2.next;
res = res.next;
}
}
while (l1 != null) {
res.next = l1;
l1 = l1.next;
res = res.next;
}
while (l2 != null) {
res.next = l2;
l2 = l2.next;
res = res.next;
}
res.next = null;
return head;
}
}