# 练习
# LeetCode:21. 合并两个有序链表
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var mergeTwoLists = function (l1, l2) {
let res = new ListNode();
let next = res;
while (l1 || l2) {
if (l1 && l2) {
if (l1.val < l2.val) {
next.next = l1;
l1 = l1.next;
} else {
next.next = l2;
l2 = l2.next;
}
next = next.next;
} else {
next.next = l1 ? l1 : l2;
break;
}
}
return res.next;
};
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
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
# LeetCode:374. 猜数字大小
/**
* Forward declaration of guess API.
* @param {number} num your guess
* @return -1 if num is lower than the guess number
* 1 if num is higher than the guess number
* otherwise return 0
* var guess = function(num) {}
*/
/**
* @param {number} n
* @return {number}
*/
var guessNumber = function (n) {
let low = 1;
let high = n;
while (low <= high) {
let mid = Math.floor((low + high) / 2)
let gue = guess(mid)
if (gue === -1) {
high = mid - 1;
} else if (gue === 1) {
low = mid + 1
} else {
return mid
}
}
return
};
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
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