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.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode tmp1 = null;
ListNode tmp2 =null;
if(l1==null)return l2;
if(l2==null)return l1;
if(l1.val<=l2.val){
tmp1=l1;tmp2=l2;
}else{
tmp1=l2;tmp2=l1;
}
ListNode re = tmp1;
for(;tmp1!=null;tmp1=tmp1.next){
while(tmp2!=null&&tmp1.val<=tmp2.val&&(tmp1.next==null||tmp1.next.val>=tmp2.val)){
ListNode tmp =new ListNode(tmp2.val);
tmp.next=tmp1.next;
tmp1.next=tmp;
tmp2=tmp2.next;
tmp1=tmp;
}
}return re;
}
}