Saturday, 18 April 2015

Hackerrank Insert a node at the head of a linked list solution

Problem Statement

This challenge is part of a tutorial track by MyCodeSchool and is accompanied by a video lesson.

You’re 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 head of the linked list and return the new head node. The head pointer given may be null meaning that the initial list is empty.

Source Code:

  Insert Node at the begining of a linked list  
  Initially head pointer argument could be NULL for empty list  
  Node is defined as   
  struct Node  
    int data;  
    struct Node *next;  
 return back the pointer to the head of the linked list in the below method.  
 Node* Insert(Node *head,int data)  
  // Complete this method  
   Node *cur = new Node;  
   cur ->data = data;  
   cur ->next = NULL;  
   if(head == NULL ){  
     head = cur;  
     cur ->next = head;  
     head = cur;  
   return head;  

** The above solution is my own code and it may not be the optimal solution or optimal way to approach the problem but it passes all the testcases in Hackerrank. So if you have any optimal approaches feel free to paste the code as the comment below..... :) :) :)


  1. Well, I'm not a specialist in this sphere, but my devs , who are starting data room in my company now, asked to say thank you. So, thank you for this code
    Waiting for some your new posts!

  2. In my opinion the right decision if you want to make money pleasant casino sites come here and get real money