We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 9f0cad5 commit 6f6feccCopy full SHA for 6f6fecc
src/data-structures/linked-list/LinkedList.js
@@ -288,4 +288,25 @@ export default class LinkedList {
288
289
return res;
290
}
291
+
292
+ /**
293
+ * Find the index of the value.
294
+ * @param {*} value
295
+ * @returns {number}
296
+ */
297
+ indexOf(value) {
298
+ if (this.getSize() === 0) return -1; // check if the size of list is 0 return -1;
299
+ let currNode = this.head;
300
+ let index = 0;
301
302
+ // Iterate the list to find the value.
303
+ while (currNode) {
304
+ if (currNode.value === value) return index;
305
+ currNode = currNode.next;
306
+ index++;
307
+ }
308
309
+ // if the value dont exist:
310
+ return -1;
311
312
0 commit comments