21. 合并两个有序链表

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/merge-two-sorted-lists
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode list1 = l1;
ListNode list2 = l2;
ListNode head = new ListNode();
ListNode cur = head;
while(list1 != null && list2 != null) {
if(list1.val < list2.val) {
cur.next = list1;
list1 = list1.next;
} else {
cur.next = list2;
list2 = list2.next;
}
cur = cur.next;
}
if(list1!= null) {
cur.next = list1;
}
if(list2!=null) {
cur.next = list2;
}
return head.next;
}
浙公网安备 33010602011771号