Description:
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.
My code:
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var mergeTwoLists = function(l1, l2) {
let min = 0;
let newList = new ListNode(0);
let head = newList;
while(l1 && l2) {
if(l1.val < l2.val) {
min = l1.val;
l1 = l1.next;
} else {
min = l2.val;
l2 = l2.next;
}
let temp = new ListNode(min);
head.next = temp;
head = head.next;
}
head.next = l1? l1: l2;
return newList.next;
};
Note: 原本是用一個(gè)min = 0去與l1跟l2比較的,然后發(fā)現(xiàn)思路不對(duì),直接讓l1.val跟l2.val比較拢操,較小的直接給head.next就可以了……
P.S. 這提submit以后pending好久瓮增,不知道是都這樣還是只有我會(huì)纫普,第一次見(jiàn)到pending那么久的驻民,還以為超時(shí)了……