I'm trying to answer Hackeranks data structure question and i don't know why this function fails

This is the question and my solution in Javascript

You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty.

// Complete the insertNodeAtTail function below.

/*

  • For your reference:
  • SinglyLinkedListNode {
  • int data;
    
  • SinglyLinkedListNode next;
    
  • }

*/
function insertNodeAtTail(head, data) {

if(!head) return;

let currentNode = head;
while(currentNode.next){
    currentNode = currentNode.next
    
}
const newNode = new SinglyLinkedListNode(data)
currentNode.next = newNode

return head

The solution, is working on my vscode but not on hackerrank

1 thought on “I'm trying to answer Hackeranks data structure question and i don't know why this function fails”

  1. 729333 185611I just could not go away your site prior to suggesting that I truly enjoyed the regular info an individual supply to your visitors? Is gonna be again continuously in order to look at new posts 45307

    Reply

Leave a Comment