Skip to content

Commit 1416973

Browse files
Merge pull request #310 from lichao0202/optimize-singlylinkedlist
JavaScript-Class06-SinglyLinkedList优化
2 parents c96dd3d + 9acaf9e commit 1416973

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

javascript/06_linkedlist/SinglyLinkedList.js

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,35 @@ class LinkedList {
1515
}
1616
// 根据value查找节点
1717
findByValue (item) {
18-
let currentNode = this.head
18+
let currentNode = this.head.next
1919
while (currentNode !== null && currentNode.element !== item) {
2020
currentNode = currentNode.next
2121
}
2222
console.log(currentNode)
2323
return currentNode === null ? -1 : currentNode
2424
}
2525

26-
// 根据index查找节点
26+
// 根据index查找节点,下标从0开始
2727
findByIndex (index) {
28-
let currentNode = this.head
28+
let currentNode = this.head.next
2929
let pos = 0
3030
while (currentNode !== null && pos !== index) {
3131
currentNode = currentNode.next
3232
pos++
3333
}
3434
console.log(currentNode)
3535
return currentNode === null ? -1 : currentNode
36-
}
36+
}
37+
38+
// 向链表末尾追加节点
39+
append(newElement) {
40+
const newNode = new Node(newElement)
41+
let currentNode = this.head
42+
while(currentNode.next) {
43+
currentNode = currentNode.next
44+
}
45+
currentNode.next = newNode
46+
}
3747

3848
// 指定元素向后插入
3949
insert (newElement, element) {
@@ -61,18 +71,17 @@ class LinkedList {
6171

6272
// 根据值删除
6373
remove (item) {
64-
const desNode = this.findByValue(item)
65-
if (desNode === -1) {
74+
const prevNode = this.findPrev(item)
75+
if (prevNode === -1) {
6676
console.log('未找到元素')
6777
return
6878
}
69-
const prevNode = this.findPrev(item)
70-
prevNode.next = desNode.next
71-
}
79+
prevNode.next = prevNode.next.next
80+
}
7281

7382
// 遍历显示所有节点
7483
display () {
75-
let currentNode = this.head
84+
let currentNode = this.head.next // 忽略头指针的值
7685
while (currentNode !== null) {
7786
console.log(currentNode.element)
7887
currentNode = currentNode.next
@@ -81,14 +90,24 @@ class LinkedList {
8190
}
8291
// Test
8392
const LList = new LinkedList()
84-
LList.insert('chen', 'head')
85-
LList.insert('curry', 'chen')
86-
LList.insert('sang', 'head')
87-
LList.insert('zhao', 'head')
93+
LList.append('chen')
94+
LList.append('curry')
95+
LList.append('sang')
96+
LList.append('zhao') // chen -> curry -> sang -> zhao
97+
console.log('-------------insert item------------')
98+
LList.insert('qian', 'chen') // 首元素后插入
99+
LList.insert('zhou', 'zhao') // 尾元素后插入
100+
LList.display() // chen -> qian -> curry -> sang -> zhao -> zhou
88101
console.log('-------------remove item------------')
89102
LList.remove('curry')
90-
LList.display()
103+
LList.display() // chen -> qian -> sang -> zhao -> zhou
91104
console.log('-------------find by item------------')
92105
LList.findByValue('chen')
93106
console.log('-------------find by index------------')
94107
LList.findByIndex(2)
108+
console.log('-------------与头结点同值元素测试------------')
109+
LList.insert('head', 'sang')
110+
LList.display() // chen -> qian -> sang -> head -> zhao -> zhou
111+
LList.findPrev('head') // sang
112+
LList.remove('head')
113+
LList.display() // chen -> qian -> sang -> zhao -> zhou

0 commit comments

Comments
 (0)