We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Data Structures
  3. Linked Lists
  4. Insert a node at the head of a linked list
  5. Discussions

Insert a node at the head of a linked list

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 862 Discussions, By:

recency

Please Login in order to post a comment

  • sabsfilho
    9 hours ago+ 0 comments

    c# compilation is not working. the same code in java works fine.

    0|
    Permalink
  • antoniojsp
    2 weeks ago+ 0 comments
    def insertNodeAtHead(llist, data):
        # Write your code here
        new_node = SinglyLinkedListNode(data)
    
        if not llist:
            return new_node
        
        new_node.next = llist
    
        return new_node
    
    0|
    Permalink
  • alex_college_al1
    2 weeks ago+ 0 comments

    Java 8 Solution:

    static SinglyLinkedListNode insertNodeAtHead(SinglyLinkedListNode llist, int data) {
        SinglyLinkedListNode newNode = new SinglyLinkedListNode(data);
        if(llist != null)
            newNode.next = llist;
        llist = newNode;
        return llist;
    }
    
    0|
    Permalink
  • anamch167447
    2 weeks ago+ 0 comments

    Great stuff, i am very imppressed with your amazing work, now could you please have a look on my site and let me know how can i use this code on my site please?>

    0|
    Permalink
  • 22mca036
    3 weeks ago+ 0 comments

    little bit confusing for beginner I have face the same issue but dont' worry guys take a time and understand the whole code you will know how to work. for me i do not understand what's happing in llist and why tail is create but after read line by line. I have understood the code and end of that. I have solve the problem.

    this is my c++ solution :

    SinglyLinkedListNode* insertNodeAtHead(SinglyLinkedListNode* llist, int data) {

    SinglyLinkedListNode* newNode = new SinglyLinkedListNode(data);
    
    if(llist == nullptr){
        llist =  newNode;
    }
    else{
        newNode->next = llist; 
        llist = newNode;
    }   
    return llist;   
    

    }

    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy