Skip to content

Instantly share code, notes, and snippets.

@thedineshj
Last active August 10, 2018 17:22
Show Gist options
  • Save thedineshj/1c1de6cf9c27ce10960d7ac556da6451 to your computer and use it in GitHub Desktop.
Save thedineshj/1c1de6cf9c27ce10960d7ac556da6451 to your computer and use it in GitHub Desktop.
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