合并有序链表需要注意边界值:
1、链表为空的特殊值需要注意处理
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode l3 = new ListNode(0);
l3.next=l1;
ListNode prel1=l3;
if(l1==null)return l2;
if(l2==null)return l1;
while(l1!=null&&l2!=null){
if(l1.val<=l2.val){
prel1=l1;
l1=l1.next;
}else{
ListNode temp= l2.next;
l2.next = l1;
prel1.next = l2;
prel1=l2;
l2=temp;
temp=null;
}
}
if(l1==null){
prel1.next = l2;
}
return l3.next;
}
}