From 9f0cad54006db7baa48b168978ccc000e5127cad Mon Sep 17 00:00:00 2001 From: AmirReza-alt Date: Mon, 4 Aug 2025 11:59:27 +0330 Subject: [PATCH 1/4] add the getSize() method --- src/data-structures/linked-list/LinkedList.js | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/data-structures/linked-list/LinkedList.js b/src/data-structures/linked-list/LinkedList.js index ba7d0e3ee1..8a3b6c1111 100644 --- a/src/data-structures/linked-list/LinkedList.js +++ b/src/data-structures/linked-list/LinkedList.js @@ -239,7 +239,9 @@ export default class LinkedList { * @return {string} */ toString(callback) { - return this.toArray().map((node) => node.toString(callback)).toString(); + return this.toArray() + .map((node) => node.toString(callback)) + .toString(); } /** @@ -269,4 +271,21 @@ export default class LinkedList { return this; } + + /** + * Get us the size of the link; + * @returns {number} + */ + getSize() { + let res = 0; + let currNode = this.head; + + // Iterate the entire list. + while (currNode) { + res++; + currNode = currNode.next; + } + + return res; + } } From 6f6fecc96c440eebfbe8d1060c67b005bf336fd5 Mon Sep 17 00:00:00 2001 From: AmirReza-alt Date: Mon, 4 Aug 2025 12:00:19 +0330 Subject: [PATCH 2/4] add the indexOf() method --- src/data-structures/linked-list/LinkedList.js | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/data-structures/linked-list/LinkedList.js b/src/data-structures/linked-list/LinkedList.js index 8a3b6c1111..a381dd9f62 100644 --- a/src/data-structures/linked-list/LinkedList.js +++ b/src/data-structures/linked-list/LinkedList.js @@ -288,4 +288,25 @@ export default class LinkedList { return res; } + + /** + * Find the index of the value. + * @param {*} value + * @returns {number} + */ + indexOf(value) { + if (this.getSize() === 0) return -1; // check if the size of list is 0 return -1; + let currNode = this.head; + let index = 0; + + // Iterate the list to find the value. + while (currNode) { + if (currNode.value === value) return index; + currNode = currNode.next; + index++; + } + + // if the value dont exist: + return -1; + } } From 215fb21f0bc5d3f26148c2d20930688357f70df2 Mon Sep 17 00:00:00 2001 From: AmirReza-alt Date: Mon, 4 Aug 2025 12:00:48 +0330 Subject: [PATCH 3/4] add the at() method --- src/data-structures/linked-list/LinkedList.js | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/data-structures/linked-list/LinkedList.js b/src/data-structures/linked-list/LinkedList.js index a381dd9f62..82bb90b006 100644 --- a/src/data-structures/linked-list/LinkedList.js +++ b/src/data-structures/linked-list/LinkedList.js @@ -309,4 +309,24 @@ export default class LinkedList { // if the value dont exist: return -1; } + + /** + * Find the value of specified index. + * @param {number} index + * @returns {*} + */ + at(index) { + // check if the index not exist + if (index < 0 || index > this.getSize()) return undefined; + let i = 0; + let currNode = this.head; + + // Iterate the list to find the index. + while (i < index) { + currNode = currNode.next; + i++; + } + + return currNode.value; + } } From 046e803669469e2ae51087e61d7850df3a442a45 Mon Sep 17 00:00:00 2001 From: AmirReza-alt Date: Mon, 4 Aug 2025 12:01:10 +0330 Subject: [PATCH 4/4] add the print() method --- src/data-structures/linked-list/LinkedList.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/data-structures/linked-list/LinkedList.js b/src/data-structures/linked-list/LinkedList.js index 82bb90b006..9c3ea6be5a 100644 --- a/src/data-structures/linked-list/LinkedList.js +++ b/src/data-structures/linked-list/LinkedList.js @@ -329,4 +329,23 @@ export default class LinkedList { return currNode.value; } + + /** + * Print the entire list. + */ + print() { + if (this.getSize() === 0) return ''; // check if the list size is zero; + let res = ''; + let currNode = this.head; + + // Iterate the list to add values to res variable + while (currNode) { + res += `${currNode.value} ==> `; + currNode = currNode.next; + } + + res += `null`; + + console.log(res); + } }