Insert a Node at the Tail of a Linked List Hackerrank solution

Insert a Node at the Tail of a Linked List Hackerrank

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. The given head pointer may be null, meaning that the initial list is empty.

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

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

Sample Input

NULL, data = 2
2 –> NULL, data = 3

Sample Output

2 –>NULL
2 –> 3 –> NULL
Explanation
1. We have an empty list, and we insert 2.
2. We start with a 2 in the tail. When 3 is inserted, 3 then becomes the tail.

Solution of Insert a Node at the Tail of a Linked List Hackerrank

To insert a node at the tail, we need to find the last node and insert a new node after this node and update the next pointer of this node.

/*
  Insert Node at the end of a linked list 
  head pointer input could be NULL as well for empty list
  Node is defined as 
  struct Node
  {
     int data;
     struct Node *next;
  }
*/
Node* Insert(Node *head,int data)
{
   Node *tmp = new Node();
    tmp->data = data;
        tmp->next = NULL;
if(head == NULL) {
    head = tmp;
    return head;
} 

Node *current = head;
while(current->next != NULL) {
    current = current->next;
}
current->next = tmp;
return head;
  // Complete this method
}
/*
  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;
  }
*/

Node Insert(Node head,int x) {
// This is a "method-only" submission. 
// You only need to complete this method. 
    Node n = new Node();
    n.data = x;
    n.next = null;
    
    if(head == null){
        head = n;
        return head;
    }
    
    else
    {
        Node c = head;
        while(c.next != null){
            c = c.next;
        }
        
        c.next = n;
        
        return head;
        
    }
  
}
"""
 Insert Node at the end of a linked list 
 head pointer 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
"""


def Insert(head, data):
    new_node = Node(data)
    if head:
        node = head
        while node.next:
            node = node.next
        node.next = new_node
    else:
        head = new_node
    return head

  
  


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

More Linked List Problem

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.