//2021-2-29
//剑指offer 简单
//合并两个排序的链表
public class Solution13 {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1==null){
return l2;
}else if(l2==null){
return l1;
}
ListNode newList = new ListNode(1);
ListNode head = newList;
ListNode p = l1;
ListNode q = l2;
while(l1!=null&& l2!=null){
if(p.val < q.val ){
l1 = l1.next;
newList.next = p;
newList = p;
p = l1;
}else{
l2= l2.next;
newList.next = q;
newList = q;
q = l2;
}
}
if(l1 == null){
while(l2!=null){
l2= l2.next;
newList.next = q;
newList = q;
q = l2;
}
}else if(l2 == null){
while(l1!=null){
l1 = l1.next;
newList.next = p;
newList = p;
p = l1;
}
}
return head;
}
}