Insert a Node at the head of a Linked List


Problem Statement :


Given a pointer to the head of a linked list, insert a new node before the head. The next value in the new node should point to head and the data value should be replaced with a given value. Return a reference to the new head of the list. The head pointer given may be null meaning that the initial list is empty.

Function Description:

Complete the function insertNodeAtHead in the editor below.

insertNodeAtHead has the following parameter(s):
      1. SinglyLinkedListNode llist: a reference to the head of a list
      2. data: the value to insert in the data field of the new node


Input Format:

The first line contains an integer n, the number of elements to be inserted at the head of the list.
The next n lines contain an integer each, the elements to be inserted, one per function call.


Constraints:
      1.   1<=n<=1000
      2.   1<=list[i]<=1000



Solution :



title-img


                            Solution in C :

In C:

//The following function is all you need to complete the 
// challenge in hackerrank platform

SinglyLinkedListNode* insertNodeAtHead(SinglyLinkedListNode* llist, int data) {
    
    SinglyLinkedListNode* new = malloc(sizeof(SinglyLinkedListNode));
    new->data = data;
    
    if(llist == NULL){
        llist = new;
        new->next = NULL;
        return llist; 
    }
    
    new-> next = llist;
    llist = new;
    return llist;

}







In C++:

//The following function is all you need to complete the challenge 
//in hackerrank platform.

Node* Insert(Node *head,int data)
{
  Node *n = new Node();
  n->data = data;
  n->next=head;
  return head = n;  
}








In Java:

//the following method is all you need to complete the challenge in
//hackerrank platform.

 static SinglyLinkedListNode insertNodeAtHead(SinglyLinkedListNode head, int data) {
        SinglyLinkedListNode node = new SinglyLinkedListNode(data);
        if (head == null) {
            return node;
        } else {
            node.next = head;
            head = node;
            return head;
        }
    }








In Python 3:

# the following method is all that is needed to  complete the 
# challenge in hackerrank platform

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








View More Similar Problems

Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function

View Solution →

Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

View Solution →

Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

View Solution →

Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →

Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →

Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →