-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeletelinklist.js
More file actions
66 lines (63 loc) · 1.4 KB
/
deletelinklist.js
File metadata and controls
66 lines (63 loc) · 1.4 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} node
* @return {void} Do not return anything, modify node in-place instead.
*/
// console.log("before", nodehead);
var deleteNode = function(node) {
while (node.next.next !== null) {
node.val = node.next.val;
node = node.next;
}
node.val = node.next.val;
node.next = null;
};
//索引引用,减少next 的次数 还不如第一种
var deleteNode_v1 = function(node) {
let n = node.next,
nn = n.next;
while (nn !== null) {
node.val = n.val;
node = n;
n = n.next;
nn = n.next;
}
node.val = node.next.val;
node.next = null;
};
// console.log("after", nodehead);
//生成链表
function glink(arr) {
var node = function(val, next) {
this.val = val;
this.next = next;
};
let head = null;
for (let i = arr.length; i > 0; i--) {
head = new node(arr[i - 1], head);
}
return head;
}
function gArr(length) {
let res = [];
while (length) {
res.push(Math.random());
length--;
}
return res;
}
//TDD
let h = glink(gArr(10000));
let h1 = glink(gArr(10000));
console.time("o");
for (let i = 0; i < 100; i++) deleteNode(h);
console.timeEnd("o");
console.time("1");
for (let i = 0; i < 100; i++) deleteNode_v1(h1);
console.timeEnd("1");