Last active
August 10, 2018 17:32
-
-
Save thedineshj/821e356aa24659833d74c3d541855ed1 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
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