/* public class ListNode { int val; ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/ //葫芦串方法从小到大将两个链表窜起来 public class Solution { public ListNode Merge(ListNode list1,ListNode list2) { //新链表的头结点 ListNode head = new ListNode(0); ListNode next = head; //两个链表都不为空的情况下循环遍历 while(list1 != null && list2 != null){
if(list1.val < list2.val){
//改变list1
next.next = list1;
list1 = list1.next;
next = next.next;
}
else{
//改变list2
next.next = list2;
list2 = list2.next;
next = next.next;
}
}
//list1剩下部分链接到新链表上
if(list1 != null){
next.next = list1;
}
//list2剩下部分连接到新链表上
if(list2 != null){
next.next = list2;
}
return head.next;
}
} //空间复杂度为O(1) //时间复杂度为O(m+n)