1、力扣原题
- 思路
- 双指针,迭代,创建一个虚拟头结点的链表,比较大小
- 最后一个指向要特殊处理
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode temp = new ListNode(-1);
ListNode newList = temp;
while (list1 != null && list2 != null) {
if (list1.val <= list2.val) {
newList.next = list1;
list1 = list1.next;
} else {
newList.next = list2;
list2 = list2.next;
}
newList = newList.next;
}
newList.next = list1 == null ? list2 : list1;
return temp.next;
}
}