Skip to content

Instantly share code, notes, and snippets.

@thedineshj
Last active August 10, 2018 17:32
Show Gist options
  • Save thedineshj/821e356aa24659833d74c3d541855ed1 to your computer and use it in GitHub Desktop.
Save thedineshj/821e356aa24659833d74c3d541855ed1 to your computer and use it in GitHub Desktop.
insertAfter(key, data) {
/*
If list is empty
*/
if (this.head == null) {
console.log("The list is empty");
}
/*
Travese through the list until `key`
matches with `data` of the `currentnode`
and then point `next` of newly created `node`
to the `next` of `cuurentnode` and then point `next`
of `currentnode` to the newly created `node`.
*/
else {
let currentNode = this.head;
let newNode = new node(data);
while (currentNode != null) {
if (currentNode.data == key) {
// point `next` of newly created `node` to the `next` of `currentnode`
newNode.next = currentNode.next;
// point `next` of `currentnode` to the newly created `node`
currentNode.next = newNode;
break;
}
currentNode = currentNode.next;
}
}
/*
length is incremented.
*/
this.length++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment