Skip to content

Commit 046e803

Browse files
committed
add the print() method
1 parent 215fb21 commit 046e803

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/data-structures/linked-list/LinkedList.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,4 +329,23 @@ export default class LinkedList {
329329

330330
return currNode.value;
331331
}
332+
333+
/**
334+
* Print the entire list.
335+
*/
336+
print() {
337+
if (this.getSize() === 0) return ''; // check if the list size is zero;
338+
let res = '';
339+
let currNode = this.head;
340+
341+
// Iterate the list to add values to res variable
342+
while (currNode) {
343+
res += `${currNode.value} ==> `;
344+
currNode = currNode.next;
345+
}
346+
347+
res += `null`;
348+
349+
console.log(res);
350+
}
332351
}

0 commit comments

Comments
 (0)