-
Notifications
You must be signed in to change notification settings - Fork 1
/
链表.js
196 lines (168 loc) · 3.58 KB
/
链表.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
class NewNode {
constructor (val) {
this.val = val
this.next = null
}
}
class NodeLink {
constructor () {
this.length = 0
this.root = null
}
addNode (node) {
if (this.root === null) {
this.root = node
} else {
let currentNode = this.root
while (currentNode.next !== null) {
currentNode = currentNode.next
}
currentNode.next = node
}
this.length++
}
removeNode (node) {
if (this.root === null) {
return -1
}
if (this.root.val === node.val) {
this.root = node.next
this.length--
return
}
let currentNode = this.root
while (currentNode.next !== null) {
if (currentNode.next.val == node.val) {
// 此操作删除节点,即指针更改
currentNode.next = currentNode.next.next
this.length--
break
}
currentNode = currentNode.next
}
}
}
const link = new NodeLink()
const a =new NewNode(1)
const b =new NewNode(2)
const c =new NewNode(3)
const d =new NewNode(4)
const e =new NewNode(5)
const extra = new NewNode(8)
link.addNode(a)
link.addNode(b)
link.addNode(c)
link.addNode(d)
link.addNode(e)
// link.removeNode(d)
// const ret = link.removeNode(extra)
// console.log(ret)
// console.log(JSON.stringify(link.root, 2), link.length)
// pre cur temp
// 1 2 3 4
// temp = cur.next
// cur.next = pre
// pre = cur
// cur = temp
function reverseLink(node) {
let pre = null
let cur = node
let temp = null
while (cur != null) {
temp = cur.next
cur.next = pre
pre = cur
cur = temp
}
return pre
}
// 查找公共元素, 链表
function getParentRoot (nodeA, nodeB) {
let pA = nodeA
let pB = nodeB
while(pA.val !== pB.val) {
pA = pA.next === null ? nodeB : pA.next
pB = pB.next === null ? nodeA : pB.next
}
return pA
}
// console.log(JSON.stringify(reverseLink(link.root), 2), link.length)
// 查找公共元素, 数组版本
const a1 = [3, 1, 2, 4, 5, 9]
const a2 = [8, 10, 6, 7, 4, 5, 9]
function getCommonEl (a1, a2) {
let i = 0
let j = 0
let temp1 = a1.concat(a2)
let temp2 = a2.concat(a1)
while (temp1[i] != temp2[j]) {
i++
j++
}
return temp2.slice(j, temp2.length)
}
const ar = getCommonEl(a1, a2)
function mergeLink (l1, l2) {
const root = {val: '', next: null}
let p = root
while (l1 && l2) {
if (l1.val < l2.val) {
p.next = l1
l1 = l1.next
} else {
p.next = l2
l2 = l2.next
}
if (p.next !== null) {
p = p.next
}
}
if (l1) {
p.next = l1
}
if (l2) {
p.next = l2
}
return root.next
}
function merge (arr) {
const flatten = [...arr]
while (flatten.length >= 2) {
const l1 = flatten.shift()
const l2 = flatten.shift()
const ret = mergeLink(l1, l2)
flatten.push(ret)
}
return flatten[0]
}
const link2 = new NodeLink();
[2, 4, 6, 8, 10].forEach(num => {
link2.addNode(new NewNode(num))
})
const link3 = new NodeLink();
[1, 3, 5, 7, 9, 11].forEach(num => {
link3.addNode(new NewNode(num))
});
const link4 = new NodeLink();
[11, 12, 13, 14, 15, 16].forEach(num => {
link4.addNode(new NewNode(num))
});
// const arr = [link2.root, link3.root, link4.root]
// const ret = merge(arr)
// console.log('ret', JSON.stringify(ret))
// const re = mergeLink(link2.root, link3.root)
// console.log('ret', JSON.stringify(re))
function reverseLink2 (link) {
let pre = null
let tmp = null
let cur = link
while (cur != null) {
tmp = cur.next
cur.next = pre
pre = cur
cur = tmp
}
return pre
}
const lins = reverseLink2(link4.root)
console.log(JSON.stringify(lins))