Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 707f24b

Browse files
Linked list excercise review
1 parent fd2a685 commit 707f24b

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

‎linkedList.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,29 @@ while (currentNode) {
5050
return null;
5151
}
5252

53+
LinkedList.prototype.indexOf = function(value) {
54+
let indexes = [];
55+
let currentIndex = 0;
56+
let currentNode = this.head;
57+
while(currentNode) {
58+
if (currentNode.value === value) {
59+
indexes.push(currentIndex);
60+
}
61+
currentNode = currentNode.next;
62+
currentIndex++;
63+
}
64+
return indexes;
65+
};
66+
67+
5368
let myLL = new LinkedList();
5469

55-
myLL.addToHead(123);
56-
myLL.addToHead(70);
57-
myLL.addToHead('hello');
58-
myLL.addToTail(19);
59-
myLL.addToTail('world');
60-
myLL.addToTail(20);
70+
myLL.addToTail(1);
71+
myLL.addToTail(5);
72+
myLL.addToTail(3);
73+
myLL.addToTail(5);
74+
myLL.addToTail(8);
75+
myLL.addToTail(7);
76+
myLL.addToTail(5);
6177

62-
console.log(myLL.search(10));
78+
console.log(myLL.indexOf(5));

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /