Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
https://leetcode.com/problems/merge-two-sorted-lists/
指针指来指去。
判断条件里错了几次,if(i.val)会把val===0的情况也排除了,要写成if(i.val !== undefined)
1 /** 2 * @param {ListNode} l1 3 * @param {ListNode} l2 4 * @return {ListNode} 5 */ 6 var mergeTwoLists = function(l1, l2) { 7 var i = l1, j = l2; 8 var res = new ListNode(-1); 9 curr = res; 10 while(i && j && i.val !== undefined && j.val !== undefined){ 11 if(i.val < j.val){ 12 curr.next = i; 13 i = i.next; 14 }else{ 15 curr.next = j; 16 j = j.next; 17 } 18 curr = curr.next; 19 } 20 if(i && i.val !== undefined){ 21 curr.next = i; 22 } 23 if(j && j.val !== undefined){ 24 curr.next = j; 25 } 26 return res.next || []; 27 };