如果觉得对你有帮助,能否点个赞或关个注,以示鼓励笔者呢?!博客目录 | 先点这里
1.题目
题目链接:传送门(点我)
2.题解
2.1非递归解法
public ListNode Merge(ListNode list1, ListNode list2) {
if(list1==null&&list2==null)return null;
if(list1==null)return list2;
if(list2==null)return list1;
ListNode minList1 = list1;
ListNode minList2 = list2;
ListNode head=null;
if(minList1.val< minList2.val){
head = list1;
minList1=minList1.next;
}else {
head=list2;
minList2=minList2.next;
}
ListNode tail = head;
while (minList1 != null && minList2 != null) {
if (minList1.val < minList2.val) {
tail.next = minList1;
minList1 = minList1.next;
} else {
tail.next = minList2;
minList2 = minList2.next;
}
tail = tail.next;
}
tail.next = null;
if (minList1 != null) tail.next = minList1;
if (minList2 != null) tail.next = minList2;
return head;
}
2.2递归解法
|