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

Insert a node at the head of a linked list Hackerrank

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.

Input Format
You have to complete the Node* Insert(Node* head, int data) method which takes two arguments – the head of the linked list and the integer to insert. You should NOT read any input from stdin/console.

Output Format
Insert the new node at the head and return the head of the updated linked list. Do NOT print anything to stdout/console.

Sample Input

NULL , data = 1
1 –> NULL , data = 2

Sample Output

1 –> NULL
2 –> 1 –> NULL
Explanation
1. We have an empty list, on inserting 1, 1 becomes new head.
2. We have a list with 1 as head, on inserting 2, 2 becomes the new head.

Solution of Insert a node at the head of a linked list hackerrank

To insert a node at the head, we need to update the next of the new node to the previous head and update the new node as the head of the linked list As head needs to be updated, we need to pass head by reference or return the new head from the function.

/*
  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)
{
    Node* tmp=new Node;
    tmp->data=data;
    tmp->next=head;
    return tmp;
    
    
  // Complete this method
}
Java solution
/*
  Insert Node at the end of a linked list 
  head pointer input could be NULL as well for empty list
  Node is defined as 
  class Node {
     int data;
     Node next;
  }
*/
// This is a "method-only" submission. 
// You only need to complete this method. 

Node Insert(Node head,int x) {
    Node n=new Node();
    n.data=x;
    n.next=null;
    
    if(head==null)
        return n;
    n.next=head;
    head=n;
    return head;
}
/*
  Insert Node at the end of a linked list 
  head pointer input could be NULL as well for empty list
  Node is defined as 
  class Node {
     int data;
     Node next;
  }
*/
// This is a "method-only" submission. 
// You only need to complete this method. 

Node Insert(Node head,int x) {
    if(head == null) return null;
    Node temp = head;
    Node first = new Node();
    first.data = x;
    head = first;
    head.next = temp;
    return head;
    
}

Python solution

"""
 Insert Node at the begining of a linked list
 head input could be None as well for empty list
 Node is defined as
 
 class Node(object):
 
   def __init__(self, data=None, next_node=None):
       self.data = data
       self.next = next_node

 return back the head of the linked list in the below method. 
"""

class Node(object):
    def __init__(self, data=None, next_node=None):
        self.data = data
        self.next = next_node

def Insert(head, data):
    if head:
        return Node(data,head)
    
    head=Node(data)
    return head
 


click here for Linked List


All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin@coderinme.com

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.