合并链表
#算法 #笔试题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
var mergeTwoLists = function(l1, l2) { let dummy = new ListNode(); let cur = dummy; while(l1&&l2){ if(l1.val>l2.val){ cur.next = l2; l2 = l2.next; }else { cur.next = l1; l1 = l1.next; } cur=cur.next; } cur.next=(l1?l1:l2); return dummy.next; };
|