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 1347c19

Browse files
author
Swastikyadav
committed
Add forEach method on linkedList class
1 parent c992ea8 commit 1347c19

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

‎18-linkedList.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,17 @@ class LinkedList {
160160

161161
prevNode.next = newNode;
162162
}
163+
164+
// forEach - Implement a forEach method on the linkedList.
165+
forEach(callBack) {
166+
let listNode = this.head;
167+
168+
while (listNode) {
169+
callBack(listNode);
170+
171+
listNode = listNode.next;
172+
}
173+
}
163174
}
164175

165176
// -------------------------
@@ -185,8 +196,13 @@ console.log(list);
185196

186197
console.log(list.getAt(1));
187198

188-
list.removeAt(1);
199+
// list.removeAt(1);
189200

190201
list.insertAt(50, 1);
191202

192-
console.log(list);
203+
console.log(list);
204+
205+
console.log(list.forEach((node) => console.log(node.data)));
206+
list.forEach((node) => node.data += 1);
207+
console.log(list);
208+
console.log(list.forEach((node) => console.log(node.data)));

0 commit comments

Comments
(0)

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