Created
December 16, 2019 17:05
-
-
Save rahul4coding/342d70c55498d02bd55cc06bd8482677 to your computer and use it in GitHub Desktop.
Insert a Node at the head of a Linked List | JavaScript
This file contains hidden or 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
//https://www.hackerrank.com/challenges/insert-a-node-at-the-head-of-a-linked-list/problem?h_r=next-challenge&h_v=zen | |
function insertNodeAtHead(head, data) { | |
var newNode = new SinglyLinkedListNode(data); | |
if(head==null){ | |
head = newNode; | |
return head; | |
}else{ | |
newNode.next = head | |
head = newNode; | |
return head; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have a different version :
addToHead(val) {
let newHeadNode = new Node(val);
newHeadNode.next = this.head;
this.head = newHeadNode;
}