Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

Hackerrank Day 15 Linked List 30 days of code solution

YASH PAL, 31 July 202412 October 2025

In this HackerRank Day 15 Linked List 30 days of code problem Complete the insert function in your editor so that it creates a new Node (pass data as the Node constructor argument) and inserts it at the tail of the linked list referenced by the head parameter. Once the new node is added, return the reference to the head node.

Objective

A Node class is provided for you in the editor. A Node object has an integer data field, data, and a Node instance pointer, next, pointing to another node (i.e.: the next node in the list).

A Node insert function is also declared in your editor. It has two parameters: a pointer, head, pointing to the first node of a linked list, and an integer, data, that must be added to the end of the list as a new Node object.

Task
Complete the insert function in your editor so that it creates a new Node (pass data as the Node constructor argument) and inserts it at the tail of the linked list referenced by the head parameter. Once the new node is added, return the reference to the head node.

Note: The head argument is null for an empty list.

Input Format

The first line contains T, the number of elements to insert.
Each of the next T lines contains an integer to insert at the end of the list.

Output Format

Return a reference to the head node of the linked list.

Problem solution in Python 2 programming.

    def insert(self,head,data): 
        #Complete this method
        node = Node(data)
        if head == None:
            head = node
        else:
            current = head
            while current.next:
                current = current.next
            current.next = node
        return head

Problem solution in Python 3.   

    def insert(self,head,data): 
        temp = Node(data)
        if head is None:
            head = temp
            return head
        current = head
        while current.next is not None:
            current = current.next
        current.next = temp
        return head       

 

Problem solution in java.

    public static  Node insert(Node head,int data){
        // if list has no elements, return a new node
        if(head == null){
            return new Node(data);
        }
        
        // else iterate through list, add node to tail, and return head
        Node tmp = head;
        while(tmp.next != null){
            tmp = tmp.next;
        }
        tmp.next = new Node(data);
            
        return head;
	}

 

Problem solution in C++.

      Node* insert(Node *head,int data)
      {
          //Complete this method
          if(head == nullptr){
              return new Node(data);
          }
          
          Node *temp = head;
          while(temp->next != nullptr){
              temp = temp->next;
          }
          
          temp->next = new Node(data);          
          return head;
      }

 

Problem solution in C.

Node* insert(Node *head,int data)
{
    //Complete this function
    struct Node *ll, *it;
    ll=(struct Node *)malloc(sizeof(struct Node));
    ll->data=data;
    ll->next=NULL;
    
    if(head==NULL)
        {
        head=ll;
    }
    else
        {
        it=head;
        while(it->next)
            it=it->next;
        it->next=ll;
    }
    return head;
}

 

Problem solution in Javascript.

    this.insert=function(head,data){
        //complete this method
        var newNode = new Node(data);

        if (head === null || typeof head === 'undefined') {
            head = newNode;  
        } else if (head.next === null) {
            head.next = newNode;
        } else {
            var next = head.next;
            while(next.next) {
                next = next.next
            }
            next.next = newNode;
        }
        
        return head;
    };

 

30 days of code coding problems solutions HackerRank

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes