|
10 | 10 | const log = console.log
|
11 | 11 |
|
12 | 12 | class Node {
|
13 |
| - constructor(element) { |
14 |
| - this.element = element; |
15 |
| - this.next = null; |
16 |
| - } |
| 13 | + constructor(element) { |
| 14 | + this.element = element; |
| 15 | + this.next = null; |
| 16 | + } |
17 | 17 | }
|
18 | 18 |
|
19 | 19 | class LinkedList {
|
20 |
| - constructor() { |
21 |
| - this.length = 0; |
22 |
| - this.head = null; |
23 |
| - } |
24 |
| - |
25 |
| - /** 向列表尾部添加元素*/ |
26 |
| - append(element) { |
27 |
| - const node = new Node(element); |
28 |
| - let current = null; |
29 |
| - |
30 |
| - if (!this.head) { |
31 |
| - this.head = node; |
32 |
| - } else { |
33 |
| - current = this.head; |
34 |
| - |
35 |
| - // 找到最末尾的元素 |
36 |
| - while (current.next) { |
37 |
| - current = current.next; |
38 |
| - } |
39 |
| - current.next = node; |
40 |
| - } |
41 |
| - |
42 |
| - this.length++; |
43 |
| - } |
44 |
| - |
45 |
| - /** 向特定位置插入元素 |
46 |
| - * @param {Number} position 要将元素插入链表的位置 |
47 |
| - * @param {any} |
48 |
| - * @return {Boolean} 返回插入状态 |
49 |
| - */ |
50 |
| - insert(position, element) { |
51 |
| - const node = new Node(element); |
52 |
| - let current = this.head, |
53 |
| - previous = null; |
54 |
| - |
55 |
| - const isValidPosition = |
56 |
| - (typeof position === 'number') && (position >= 0) && (position < this.length); |
57 |
| - if (!isValidPosition) { return false } |
58 |
| - |
59 |
| - if (position === 0) { |
60 |
| - node.next = current; |
61 |
| - this.head = node; |
62 |
| - } else { |
63 |
| - for (let i = 0; i < position; i++) { |
64 |
| - previous = current; |
65 |
| - current = current.next; |
66 |
| - } |
67 |
| - |
68 |
| - node.next = current; |
69 |
| - previous.next = node; |
70 |
| - } |
71 |
| - this.length++; |
72 |
| - return true; |
73 |
| - } |
74 |
| - |
75 |
| - /** 移除指定元素 |
76 |
| - * @return 移除成功时,返回被移除的元素 |
77 |
| - */ |
78 |
| - remove(element) { |
79 |
| - let current = this.head, |
80 |
| - previous = null; |
81 |
| - |
82 |
| - if (this.head.element === element) { |
83 |
| - this.head = current.next; |
84 |
| - this.length--; |
85 |
| - return current.element; |
86 |
| - } |
87 |
| - |
88 |
| - while (current.next) { |
89 |
| - previous = current; |
90 |
| - current = current.next; |
91 |
| - |
92 |
| - if (current.element === element) { |
93 |
| - previous.next = current.next; |
94 |
| - this.length--; |
95 |
| - return current.element; |
96 |
| - } |
97 |
| - } |
98 |
| - |
99 |
| - return null; |
100 |
| - } |
101 |
| - |
102 |
| - /** 从列表的特定位置移除一项 |
103 |
| - * @return 移除成功时,返回被移除的元素 |
104 |
| - */ |
105 |
| - removeAt(position) { |
106 |
| - const isValidPosition = |
107 |
| - (typeof position === 'number') && (position >= 0) && (position < this.length); |
108 |
| - |
109 |
| - if (isValidPosition) { |
110 |
| - let current = this.head, |
111 |
| - previous = null; |
112 |
| - |
113 |
| - if (position === 0) { |
114 |
| - this.head = current.next; |
115 |
| - } else { |
116 |
| - for (let i = 0; i < position; i++) { |
117 |
| - previous = current; |
118 |
| - current = current.next; |
119 |
| - } |
120 |
| - |
121 |
| - // 将 current node 从链表 链中移除 |
122 |
| - previous.next = current.next; |
123 |
| - } |
124 |
| - this.length--; |
125 |
| - return current.element; |
126 |
| - } |
127 |
| - |
128 |
| - if (!isValidPosition) { return null } |
129 |
| - } |
130 |
| - |
131 |
| - /** 返回元素在列表中的索引;若无改元素,返回 -1 |
132 |
| - * |
133 |
| - */ |
134 |
| - indexOf(element) { |
135 |
| - |
136 |
| - } |
137 |
| - |
138 |
| - /** 检查链表为空时,返回 true |
139 |
| - * |
140 |
| - */ |
141 |
| - isEmpty() { |
142 |
| - |
143 |
| - } |
144 |
| - |
145 |
| - /** 返回链表元素个数 |
146 |
| - * @return {Number} |
147 |
| - */ |
148 |
| - size() { |
149 |
| - |
150 |
| - } |
151 |
| - |
152 |
| - |
| 20 | + constructor() { |
| 21 | + this.length = 0; |
| 22 | + this.head = null; |
| 23 | + } |
| 24 | + |
| 25 | + /** 向列表尾部添加元素*/ |
| 26 | + append(element) { |
| 27 | + const node = new Node(element); |
| 28 | + let current = null; |
| 29 | + |
| 30 | + if (!this.head) { |
| 31 | + this.head = node; |
| 32 | + } else { |
| 33 | + current = this.head; |
| 34 | + |
| 35 | + // 找到最末尾的元素 |
| 36 | + while (current.next) { |
| 37 | + current = current.next; |
| 38 | + } |
| 39 | + current.next = node; |
| 40 | + } |
| 41 | + |
| 42 | + this.length++; |
| 43 | + } |
| 44 | + |
| 45 | + /** 向特定位置插入元素 |
| 46 | + * @param {Number} position 要将元素插入链表的位置 |
| 47 | + * @param {any} |
| 48 | + * @return {Boolean} 返回插入状态 |
| 49 | + */ |
| 50 | + insert(position, element) { |
| 51 | + const node = new Node(element); |
| 52 | + let current = this.head, |
| 53 | + previous = null; |
| 54 | + |
| 55 | + const isValidPosition = |
| 56 | + (typeof position === 'number') && (position >= 0) && (position < this.length); |
| 57 | + if (!isValidPosition) { |
| 58 | + return false |
| 59 | + } |
| 60 | + |
| 61 | + if (position === 0) { |
| 62 | + node.next = current; |
| 63 | + this.head = node; |
| 64 | + } else { |
| 65 | + for (let i = 0; i < position; i++) { |
| 66 | + previous = current; |
| 67 | + current = current.next; |
| 68 | + } |
| 69 | + |
| 70 | + node.next = current; |
| 71 | + previous.next = node; |
| 72 | + } |
| 73 | + this.length++; |
| 74 | + return true; |
| 75 | + } |
| 76 | + |
| 77 | + /** 移除指定元素 |
| 78 | + * @return 移除成功时,返回被移除的元素 |
| 79 | + */ |
| 80 | + remove(element) { |
| 81 | + let current = this.head, |
| 82 | + previous = null; |
| 83 | + |
| 84 | + if (this.head.element === element) { |
| 85 | + this.head = current.next; |
| 86 | + this.length--; |
| 87 | + return current.element; |
| 88 | + } |
| 89 | + |
| 90 | + while (current.next) { |
| 91 | + previous = current; |
| 92 | + current = current.next; |
| 93 | + |
| 94 | + if (current.element === element) { |
| 95 | + previous.next = current.next; |
| 96 | + this.length--; |
| 97 | + return current.element; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + /** 从列表的特定位置移除一项 |
| 105 | + * @return 移除成功时,返回被移除的元素 |
| 106 | + */ |
| 107 | + removeAt(position) { |
| 108 | + const isValidPosition = |
| 109 | + (typeof position === 'number') && (position >= 0) && (position < this.length); |
| 110 | + |
| 111 | + if (isValidPosition) { |
| 112 | + let current = this.head, |
| 113 | + previous = null; |
| 114 | + |
| 115 | + if (position === 0) { |
| 116 | + this.head = current.next; |
| 117 | + } else { |
| 118 | + for (let i = 0; i < position; i++) { |
| 119 | + previous = current; |
| 120 | + current = current.next; |
| 121 | + } |
| 122 | + |
| 123 | + // 将 current node 从链表 链中移除 |
| 124 | + previous.next = current.next; |
| 125 | + } |
| 126 | + this.length--; |
| 127 | + return current.element; |
| 128 | + } |
| 129 | + |
| 130 | + if (!isValidPosition) { |
| 131 | + return null |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /** 返回元素在列表中的索引;若无改元素,返回 -1 |
| 136 | + * |
| 137 | + */ |
| 138 | + indexOf(element) { |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | + /** 检查链表为空时,返回 true |
| 143 | + * |
| 144 | + */ |
| 145 | + isEmpty() { |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | + /** 返回链表元素个数 |
| 150 | + * @return {Number} |
| 151 | + */ |
| 152 | + size() { |
| 153 | + |
| 154 | + } |
153 | 155 |
|
154 | 156 | }
|
155 | 157 |
|
|
0 commit comments