Skip to content

Instantly share code, notes, and snippets.

@thedineshj
Last active August 10, 2018 17:21
Show Gist options
  • Save thedineshj/faa957ef5ffdf2d72f68e8d4104b5e8d to your computer and use it in GitHub Desktop.
Save thedineshj/faa957ef5ffdf2d72f68e8d4104b5e8d to your computer and use it in GitHub Desktop.
addToHead(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,
we will point `next`
of the newly created `node`
to the current `head` of the list
and then set `head` of the list to newly created node
*/
else {
newNode.next = this.head;
this.head = newNode;
}
/*
increment length
*/
this.length++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment