|
1 | 1 | <script> |
2 | | - /** 时间复杂度O(1) */ |
3 | | - function fnO1() { |
4 | | - console.log("时间复杂度O(1)"); |
| 2 | + // /** 时间复杂度O(1) */ |
| 3 | + // function fnO1() { |
| 4 | + // console.log("时间复杂度O(1)"); |
5 | 5 |
|
6 | | - const a = 5; |
7 | | - const b = 10; |
8 | | - const sum = a + b; |
9 | | - } |
| 6 | + // const a = 5; |
| 7 | + // const b = 10; |
| 8 | + // const sum = a + b; |
| 9 | + // } |
10 | 10 |
|
11 | | - /** 时间复杂度 O(logn)、 O(nlogn) */ |
| 11 | + // /** 时间复杂度 O(logn)、 O(nlogn) */ |
12 | 12 |
|
13 | | - function fnOlogn(n) { |
14 | | - console.log("时间复杂度O(logn)"); |
| 13 | + // function fnOlogn(n) { |
| 14 | + // console.log("时间复杂度O(logn)"); |
15 | 15 |
|
16 | | - let count = 0; |
17 | | - let i = 1; |
18 | | - while (i < n) { |
19 | | - i = i * 2; |
20 | | - count++; |
21 | | - } |
| 16 | + // let count = 0; |
| 17 | + // let i = 1; |
| 18 | + // while (i < n) { |
| 19 | + // i = i * 2; |
| 20 | + // count++; |
| 21 | + // } |
22 | 22 |
|
23 | | - console.log("时间复杂度O(logn), 执行次数count: ", count); |
24 | | - } |
| 23 | + // console.log("时间复杂度O(logn), 执行次数count: ", count); |
| 24 | + // } |
25 | 25 |
|
26 | | - function fnOnlogn(n) { |
27 | | - console.log("时间复杂度O(nlogn)"); |
28 | | - let count = 0; |
29 | | - for (let j = 0; j < n; j++) { |
30 | | - let i = 1; |
31 | | - while (i < n) { |
32 | | - i = i * 2; |
33 | | - count++; |
34 | | - } |
35 | | - } |
36 | | - console.log("时间复杂度O(nlogn), 执行次数count: ", count); |
37 | | - } |
| 26 | + // function fnOnlogn(n) { |
| 27 | + // console.log("时间复杂度O(nlogn)"); |
| 28 | + // let count = 0; |
| 29 | + // for (let j = 0; j < n; j++) { |
| 30 | + // let i = 1; |
| 31 | + // while (i < n) { |
| 32 | + // i = i * 2; |
| 33 | + // count++; |
| 34 | + // } |
| 35 | + // } |
| 36 | + // console.log("时间复杂度O(nlogn), 执行次数count: ", count); |
| 37 | + // } |
38 | 38 |
|
39 | | - /** 时间复杂度 fnO(m+n), 当可以确认n >= m的时候,时间复杂度可以修改为 O(n) */ |
| 39 | + // /** 时间复杂度 fnO(m+n), 当可以确认n >= m的时候,时间复杂度可以修改为 O(n) */ |
40 | 40 |
|
41 | | - function fnOmn1(m, n) { |
42 | | - let countM = 0; |
43 | | - let countN = 0; |
44 | | - for (let i = 0; i < m; i++) { |
45 | | - countM += 1; |
46 | | - } |
47 | | - for (let j = 0; j < n; j++) { |
48 | | - countN += 1; |
49 | | - } |
| 41 | + // function fnOmn1(m, n) { |
| 42 | + // let countM = 0; |
| 43 | + // let countN = 0; |
| 44 | + // for (let i = 0; i < m; i++) { |
| 45 | + // countM += 1; |
| 46 | + // } |
| 47 | + // for (let j = 0; j < n; j++) { |
| 48 | + // countN += 1; |
| 49 | + // } |
50 | 50 |
|
51 | | - console.log("时间复杂度O(m+n),执行次数count: ", countM + countN); |
| 51 | + // console.log("时间复杂度O(m+n),执行次数count: ", countM + countN); |
| 52 | + // } |
| 53 | + |
| 54 | + // /** 时间复杂度 fnO(m*n), 当可以确认n >= m的时候,时间复杂度可以修改为 O(n^2) */ |
| 55 | + // function fnOmn2(m, n) { |
| 56 | + // let count = 0; |
| 57 | + // for (let i = 0; i < m; i++) { |
| 58 | + // for (let j = 0; j < n; j++) { |
| 59 | + // count += 1; |
| 60 | + // } |
| 61 | + // } |
| 62 | + |
| 63 | + // console.log("时间复杂度O(m*n),执行次数count: ", count); |
| 64 | + // } |
| 65 | + |
| 66 | + function array2List(arr, type = 0) { |
| 67 | + if (!arr.length) return null; |
| 68 | + let header = { data: arr[0], next: null }; |
| 69 | + let obj = header; |
| 70 | + for (let i = 1; i < arr.length; i++) { |
| 71 | + obj.next = { data: arr[i], next: null }; |
| 72 | + obj = obj.next; |
| 73 | + } |
| 74 | + if (type) obj.next = header; |
| 75 | + return header; |
52 | 76 | } |
53 | 77 |
|
54 | | - /** 时间复杂度 fnO(m*n), 当可以确认n >= m的时候,时间复杂度可以修改为 O(n^2) */ |
55 | | - function fnOmn2(m, n) { |
56 | | - let count = 0; |
57 | | - for (let i = 0; i < m; i++) { |
58 | | - for (let j = 0; j < n; j++) { |
59 | | - count += 1; |
60 | | - } |
| 78 | + var list = array2List([1, 2, 3, 4, 5]); |
| 79 | + console.log(list); |
| 80 | + |
| 81 | + var reverseList = function (head) { |
| 82 | + console.log(head); |
| 83 | + let p1 = head; |
| 84 | + let p2 = null; |
| 85 | + while (p1) { |
| 86 | + const tmp = p1.next; |
| 87 | + p1.next = p2; |
| 88 | + p2 = p1; |
| 89 | + p1 = tmp; |
| 90 | + // p2 = p1; |
| 91 | + // p1 = p1.next; |
61 | 92 | } |
| 93 | + return p2; |
| 94 | + }; |
62 | 95 |
|
63 | | - console.log("时间复杂度O(m*n),执行次数count: ", count); |
64 | | - } |
| 96 | + var result = reverseList(list); |
| 97 | + console.log(result); |
65 | 98 | </script> |
0 commit comments