Last active
August 10, 2018 17:22
-
-
Save thedineshj/1c1de6cf9c27ce10960d7ac556da6451 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
addToLast(data) { | |
let newNode = new node(data); | |
/* | |
If the list is empty , | |
newly created `node` | |
will be the `head` of the list. | |
*/ | |
if (this.head == null) { | |
this.head = newNode; | |
} | |
/* | |
if not,traverse through the list recursively | |
until `next` of the `currentnode` is null | |
and then point `next` of the `currentnode` | |
to the newly created `node`. | |
*/ | |
else { | |
let currentNode = this.head; | |
while (currentNode.next != null) { | |
currentNode = currentNode.next; | |
} | |
currentNode.next = newNode; | |
} | |
/* | |
increment length | |
*/ | |
this.length++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment