Skip to content

Commit eb61a2d

Browse files
committed
adding forEach method and for...of loop
1 parent eea8c32 commit eb61a2d

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

linkedList/linkedList.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,5 +141,23 @@ class LinkedList {
141141
const node = new Node(data, previous.next);
142142
previous.next = node;
143143
}
144+
145+
forEach(fn) {
146+
let node = this.head;
147+
let counter = 0;
148+
while (node) {
149+
fn(node, counter);
150+
node = node.next;
151+
counter++;
152+
}
153+
}
154+
155+
*[Symbol.iterator]() {
156+
let node = this.head;
157+
while (node) {
158+
yield node;
159+
node = node.next;
160+
}
161+
}
144162
}
145163

0 commit comments

Comments
 (0)