Skip to content

Commit 7611a01

Browse files
committed
优化目录结构
1 parent 27d8ec8 commit 7611a01

File tree

5 files changed

+181
-65
lines changed

5 files changed

+181
-65
lines changed

main.js

Lines changed: 8 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ const b = new ListNode(2)
88
const c = new ListNode(4)
99
a.next = b
1010
b.next = c
11+
12+
c.next = a;
13+
14+
console.log( isCircularLinkList(a) )
15+
1116
const d = new ListNode(1)
1217
const e = new ListNode(3)
1318
const f = new ListNode(4)
@@ -24,70 +29,8 @@ function bianli(root, vals = []) {
2429
// bianli(a)
2530
// bianli(d)
2631

27-
/** 迭代法
28-
* @param {ListNode} l1
29-
* @param {ListNode} l2
30-
* @return {ListNode}
31-
*/
32-
var mergeTwoLists = function (l1, l2) {
33-
let res = new ListNode();
34-
let root = res
35-
while (l1 !== null && l2 !== null) {
36-
if (l1.val <= l2.val) {
37-
root.next = l1;
38-
l1 = l1.next;
39-
} else {
40-
root.next = l2
41-
l2 = l2.next;
42-
}
43-
root = root.next
44-
}
45-
if (l1 != null) {
46-
root.next = l1;
47-
}
4832

49-
if (l2 != null) {
50-
root.next = l2;
51-
}
52-
return res.next
53-
};
54-
55-
/**转化数组法
56-
* @param {ListNode} l1
57-
* @param {ListNode} l2
58-
* @return {ListNode}
59-
*/
60-
var mergeTwoLists2 = function (l1, l2) {
61-
if (l1 == null && l2 == null) return l1
62-
const l1res = bianli(l1)
63-
const l2res = bianli(l2)
64-
const resData = [...l1res, ...l2res].sort((a, b) => a - b)
65-
let root = new ListNode(resData[0]);
66-
let res = root;
67-
for (let i = 1; i < resData.length; i++) {
68-
res.next = new ListNode(resData[i])
69-
res = res.next;
70-
}
71-
return root
72-
};
33+
// console.log(bianli(mergeTwoLists3(a, d)))
7334

74-
75-
/**递归
76-
* @param {ListNode} l1
77-
* @param {ListNode} l2
78-
* @return {ListNode}
79-
*/
80-
var mergeTwoLists3 = function (l1, l2) {
81-
if (l1 == null) {
82-
return l2;
83-
} else if (l2 == null) {
84-
return l1;
85-
} else if (l1.val < l2.val) {
86-
l1.next = mergeTwoLists(l1.next, l2);
87-
return l1;
88-
} else {
89-
l2.next = mergeTwoLists(l1, l2.next);
90-
return l2;
91-
}
92-
};
93-
console.log(bianli(mergeTwoLists3(a, d)))
35+
import './链表/myLinkList.js'
36+
import { isCircularLinkList } from './utils/linkList.js';

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"type": "module",
23
"name": "algorithm",
34
"version": "1.0.0",
45
"description": "数据结构算法(javascript描述)",

utils/linkList.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/**
2+
* 查找链表最后一个元素
3+
* @param { ListNode } root
4+
* @returns
5+
*/
6+
export const findLinkEnd = (root) => {
7+
if (root.next == null) return root;
8+
return findLinkEnd(root.next)
9+
}
10+
/**
11+
* 通过类数组索引找到当前元素
12+
* @param {*} root
13+
* @param {*} index
14+
* @returns
15+
*/
16+
export const findLinkItemByIndex = (root, index) => {
17+
let i = 0;
18+
while (root != null) {
19+
if (i == index) {
20+
return root
21+
}
22+
i++
23+
root = root.next
24+
}
25+
return null
26+
}
27+
/**
28+
* 遍历
29+
* @param {*} root
30+
*/
31+
export const bianli = (root) => {
32+
while (root !== null) {
33+
console.log(root.val)
34+
root = root.next
35+
}
36+
}
37+
/**
38+
* 判断链表是否有换
39+
* @param {*} root
40+
* @returns
41+
*/
42+
export const isCircularLinkList = (root) => {
43+
let low = root;
44+
let quick = root;
45+
while(quick !== null ){
46+
if(quick == low) {
47+
return true
48+
}
49+
low = low.next
50+
quick = quick.next.next
51+
}
52+
return false
53+
}
54+
55+
/** 迭代法
56+
* @param {ListNode} l1
57+
* @param {ListNode} l2
58+
* @return {ListNode}
59+
*/
60+
var mergeTwoLists = function (l1, l2) {
61+
let res = new ListNode();
62+
let root = res
63+
while (l1 !== null && l2 !== null) {
64+
if (l1.val <= l2.val) {
65+
root.next = l1;
66+
l1 = l1.next;
67+
} else {
68+
root.next = l2
69+
l2 = l2.next;
70+
}
71+
root = root.next
72+
}
73+
if (l1 != null) {
74+
root.next = l1;
75+
}
76+
77+
if (l2 != null) {
78+
root.next = l2;
79+
}
80+
return res.next
81+
};
82+
83+
/**转化数组法
84+
* @param {ListNode} l1
85+
* @param {ListNode} l2
86+
* @return {ListNode}
87+
*/
88+
var mergeTwoLists2 = function (l1, l2) {
89+
if (l1 == null && l2 == null) return l1
90+
const l1res = bianli(l1)
91+
const l2res = bianli(l2)
92+
const resData = [...l1res, ...l2res].sort((a, b) => a - b)
93+
let root = new ListNode(resData[0]);
94+
let res = root;
95+
for (let i = 1; i < resData.length; i++) {
96+
res.next = new ListNode(resData[i])
97+
res = res.next;
98+
}
99+
return root
100+
};
101+
102+
103+
/**递归
104+
* @param {ListNode} l1
105+
* @param {ListNode} l2
106+
* @return {ListNode}
107+
*/
108+
var mergeTwoLists3 = function (l1, l2) {
109+
if (l1 == null) {
110+
return l2;
111+
} else if (l2 == null) {
112+
return l1;
113+
} else if (l1.val < l2.val) {
114+
l1.next = mergeTwoLists(l1.next, l2);
115+
return l1;
116+
} else {
117+
l2.next = mergeTwoLists(l1, l2.next);
118+
return l2;
119+
}
120+
};

链表/myLinkList.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { bianli, findLinkEnd, findLinkItemByIndex } from "../utils/linkList.js"
2+
3+
class ListNode {
4+
val = null
5+
next = null
6+
constructor(val = null) {
7+
this.val = val
8+
}
9+
}
10+
11+
class LinkList {
12+
root = null
13+
constructor(root) {
14+
if (root instanceof ListNode) {
15+
this.root = root
16+
} else {
17+
this.root = new ListNode()
18+
}
19+
}
20+
add(item) {
21+
if (item instanceof ListNode) {
22+
const rootEnd = findLinkEnd(this.root)
23+
rootEnd.next = item
24+
return rootEnd.next
25+
}
26+
throw new Error("传入元素不匹配")
27+
}
28+
remove(index) {
29+
if(typeof index !== "number") throw new Error("类型错误")
30+
const curr = findLinkItemByIndex(this.root, index - 1);
31+
if(curr == null || curr.next == null ){
32+
console.log("长度越界")
33+
return
34+
}
35+
const newNext = curr.next.next;
36+
curr.next = newNext
37+
}
38+
39+
}
40+
41+
const link = new LinkList()
42+
link.root.val = 1
43+
link.add(new ListNode(2))
44+
link.add(new ListNode(3))
45+
link.add(new ListNode(4))
46+
47+
link.remove(2)
48+
// console.log(link)
49+
50+
// bianli(link.root)
51+
// console.log( findLinkEnd(link.root))
52+
// console.log( findLinkItemByIndex(link.root, 4) )
File renamed without changes.

0 commit comments

Comments
 (0)