Merge two sorted linked lists and return it as a new list.
The new list should be made by splicing together the nodes of the first two lists.
是在l1的基础上连接的, 所以要注意需要链接l1
l2.next = l1;
需要不断更新前一个节点
pre = pre.next;
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1 == null) return l2;
if(l2 == null) return l1;
ListNode dummy = new ListNode(0);
dummy.next = l1;
ListNode pre = dummy;
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) {
l1 = l1.next;
} else {
ListNode next = l2.next;
//ListNode temp = pre.next;
pre.next = l2;
l2.next = l1;
l2 = next;
}
pre = pre.next;
}
if (l2 != null) {
pre.next = l2;
}
return dummy.next;
}
}