Insert a node at the head of a linked list in Java 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 SinglyLinkedListNode Insert(SinglyLinkedListNode 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.

The input is handled by code in the editor and is as follows:

The first line contains an integer n , denoting the number of elements to be inserted at the head of the list.
The next n lines contain an integer each, denoting the element to be inserted.

Constraints

  • 1 <= n <= 1000
  • 1 <= list <= 1000

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.

The output is handled by the code in the editor and it is as follows:

Print the elements of linked list from head to tail, each in a new line.

Sample Input

5
383
484
392
975
321

Sample Output

321
975
392
484
383

Explanation

Initially the list in NULL. After inserting 383, the list is 383 -> NULL.
After inserting 484, the list is 484 -> 383 -> NULL.
After inserting 392, the list is 392 -> 484 -> 383 -> NULL.
After inserting 975, the list is 975 -> 392 -> 484 -> 383 -> NULL.
After inserting 321, the list is 321 -> 975 -> 392 -> 484 -> 383 -> NULL.

Here’s the very Simple solution:

  1. First we need to simply create a new node.
  2. After creating new node assign the insert.next to head of linked list.
  3. And finally just reset the head pointer to insert (head = insert).

Code:

 // Complete the insertNodeAtHead function below.

    /*
     * For your reference:
     *
     * SinglyLinkedListNode {
     *     int data;
     *     SinglyLinkedListNode next;
     * }
     *
     */
    static SinglyLinkedListNode insertNodeAtHead(SinglyLinkedListNode llist, int data) {

        SinglyLinkedListNode insert = new SinglyLinkedListNode(data);
        insert.next = llist;
        llist = insert;
        return llist;

       


    }

Hope you guys understand the simple solution of adding a new node at head in java.

In next blog we will see other operations on linked list till then Bye Bye..!

Thanks for reading…!

Happy Coding..!

Thank You…!



Categories: HackerRank Questions, Interview, JAVA, Uncategorized

Tags: , , , , , , , ,

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: