Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9449687

Browse files
author
zhangxing
committed
feat: 链表
1 parent fee04d3 commit 9449687

File tree

91 files changed

+6405
-91
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+6405
-91
lines changed

‎.history/链表/2_20211114165730.js‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class List {
2+
constructor(head, value) {
3+
this.data = []; //结点的数据
4+
this.next = []; //此节点的下一个结点的索引
5+
this.head = head; //记录着头元素的索引
6+
this.data[head] = value; //头元素的值或者说数据
7+
}
8+
/**
9+
* 添加节点
10+
* @param {*} index 哪个索引的位置添加节点
11+
* @param {*} nextIndex 添加的节点的所在的索引
12+
* @param {*} value 添加的节点对应的值
13+
*/
14+
add(index, nextIndex, value) {
15+
//数组中index这个索引对应的值是当前索引节点的下一个结点的下标
16+
this.next[index] = nextIndex;
17+
this.data[nextIndex] = value;
18+
}
19+
print() {
20+
let curr = this.head;
21+
debugger;
22+
let str = '';
23+
while (curr !== undefined) {
24+
str += this.data[curr] + '=>';
25+
curr = this.next[curr];
26+
}
27+
str += 'null';
28+
console.log(str);
29+
}
30+
}
31+
//使用索引为2表示链表的头部
32+
//故意 不用0,因为我想证明存储空间是无序的,我可以用任何一个地位表示头部
33+
/* let head = 2;
34+
let list = new List(head, 'A');
35+
list.add(head, 4, 'B');
36+
list.add(4, 6, 'C');
37+
list.add(6, 0, 'D');
38+
list.print(); */
39+
40+
let head = 0;
41+
let list = new List(head, 'A');
42+
list.add(head, 1, 'B');
43+
list.add(1, 2, 'C');
44+
list.add(2, 3, 'D');
45+
console.log(list.next);
46+
console.log(list.data);
47+
list.print();

‎.history/链表/2_20211114170034.js‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class List {
2+
constructor(head, value) {
3+
this.data = []; //结点的数据
4+
this.next = []; //此节点的下一个结点的索引
5+
this.head = head; //记录着头元素的索引
6+
this.data[head] = value; //头元素的值或者说数据
7+
}
8+
/**
9+
* 添加节点
10+
* @param {*} index 哪个索引的位置添加节点
11+
* @param {*} nextIndex 添加的节点的所在的索引
12+
* @param {*} value 添加的节点对应的值
13+
*/
14+
add(index, nextIndex, value) {
15+
//数组中index这个索引对应的值是当前索引节点的下一个结点的下标
16+
this.next[index] = nextIndex;
17+
this.data[nextIndex] = value;
18+
}
19+
print() {
20+
let curr = this.head;
21+
let str = '';
22+
while (curr !== undefined) {
23+
str += this.data[curr] + '=>';
24+
curr = this.next[curr];
25+
}
26+
str += 'null';
27+
console.log(str);
28+
}
29+
}
30+
//使用索引为2表示链表的头部
31+
//故意 不用0,因为我想证明存储空间是无序的,我可以用任何一个地位表示头部
32+
/* let head = 2;
33+
let list = new List(head, 'A');
34+
list.add(head, 4, 'B');
35+
list.add(4, 6, 'C');
36+
list.add(6, 0, 'D');
37+
list.print(); */
38+
39+
let head = 0;
40+
let list = new List(head, 'A');
41+
list.add(head, 1, 'B');
42+
list.add(1, 2, 'C');
43+
list.add(2, 3, 'D');
44+
console.log(list.next);
45+
console.log(list.data);
46+
list.print();

‎.history/链表/LeeCode 83_20211114153429.js‎

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
2+
/**
3+
* Definition for singly-linked list.
4+
* function ListNode(val, next) {
5+
* this.val = (val===undefined ? 0 : val)
6+
* this.next = (next===undefined ? null : next)
7+
* }
8+
*/
9+
/**
10+
* @param {ListNode} head
11+
* @return {ListNode}
12+
*/
13+
var deleteDuplicates = function (head) {};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
2+
/**
3+
* Definition for singly-linked list.
4+
* function ListNode(val, next) {
5+
* this.val = (val===undefined ? 0 : val)
6+
* this.next = (next===undefined ? null : next)
7+
* }
8+
*/
9+
/**
10+
* @param {ListNode} head
11+
* @return {ListNode}
12+
*/
13+
14+
function ListNode(val, next) {
15+
this.val = val === undefined ? 0 : val;
16+
this.next = next === undefined ? null : next;
17+
}
18+
var deleteDuplicates = function (head) {};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
2+
/**
3+
* Definition for singly-linked list.
4+
* function ListNode(val, next) {
5+
* this.val = (val===undefined ? 0 : val)
6+
* this.next = (next===undefined ? null : next)
7+
* }
8+
*/
9+
/**
10+
* @param {ListNode} head
11+
* @return {ListNode}
12+
*/
13+
14+
function ListNode(val, next) {
15+
this.val = val === undefined ? 0 : val;
16+
this.next = next === undefined ? null : next;
17+
}
18+
var deleteDuplicates = function (head) {
19+
let minNode = head;
20+
let nextNode = head;
21+
while (nextNode) {
22+
if (minNode.val > nextNode.val) {
23+
let tempNode = nextNode.next;
24+
nextNode.next = minNode;
25+
minNode.next = tempNode;
26+
}
27+
if (minNode.val === nextNode.val) {
28+
minNode.next = nextNode.next;
29+
}
30+
nextNode = nextNode.next;
31+
}
32+
return head;
33+
};
34+
35+
let head = [1, 1, 2].reduce((cur, next) => {
36+
return new ListNode(cur, next);
37+
}, []);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
2+
/**
3+
* Definition for singly-linked list.
4+
* function ListNode(val, next) {
5+
* this.val = (val===undefined ? 0 : val)
6+
* this.next = (next===undefined ? null : next)
7+
* }
8+
*/
9+
/**
10+
* @param {ListNode} head
11+
* @return {ListNode}
12+
*/
13+
14+
var deleteDuplicates = function (head) {
15+
let minNode = head;
16+
let nextNode = head;
17+
while (nextNode) {
18+
if (minNode.val > nextNode.val) {
19+
let tempNode = nextNode.next;
20+
nextNode.next = minNode;
21+
minNode.next = tempNode;
22+
}
23+
if (minNode.val === nextNode.val) {
24+
minNode.next = nextNode.next;
25+
}
26+
nextNode = nextNode.next;
27+
}
28+
return head;
29+
};
30+
31+
function ListNode(val, next) {
32+
this.val = val === undefined ? 0 : val;
33+
this.next = next === undefined ? null : next;
34+
}
35+
let head = [1, 1, 2].reduce((cur, next) => {
36+
return cur.push(new ListNode(cur, next));
37+
}, []);
38+
39+
console.log(head);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
2+
/**
3+
* Definition for singly-linked list.
4+
* function ListNode(val, next) {
5+
* this.val = (val===undefined ? 0 : val)
6+
* this.next = (next===undefined ? null : next)
7+
* }
8+
*/
9+
/**
10+
* @param {ListNode} head
11+
* @return {ListNode}
12+
*/
13+
14+
var deleteDuplicates = function (head) {
15+
let minNode = head;
16+
let nextNode = head;
17+
while (nextNode) {
18+
if (minNode.val > nextNode.val) {
19+
let tempNode = nextNode.next;
20+
nextNode.next = minNode;
21+
minNode.next = tempNode;
22+
}
23+
if (minNode.val === nextNode.val) {
24+
minNode.next = nextNode.next;
25+
}
26+
nextNode = nextNode.next;
27+
}
28+
return head;
29+
};
30+
31+
function ListNode(val, next) {
32+
this.val = val === undefined ? 0 : val;
33+
this.next = next === undefined ? null : next;
34+
}
35+
let head = [1, 1, 2].reduce((cur, next) => {
36+
return cur.push(new ListNode(cur, next));
37+
}, []);
38+
39+
console.log(head);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /