Day 15: Linked List In Java | 30 Days Of Code | HackerRank Programming Solutions

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of 30 Days Of Code, HackerRank. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for Day 15: Linked List in Java-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

Link for the ProblemDay 15: Linked List – Hacker Rank Solution

Day 15: Linked List – Hacker Rank Solution

Problem:

Objective
Today we will work with a Linked List. Check out the Tutorial tab for learning materials and an instructional video.


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

Node insert function is also declared in your editor. It has two parameters: a pointer, , pointing to the first node of a linked list, and an integer, , 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  as the Node constructor argument) and inserts it at the tail of the linked list referenced by the  parameter. Once the new node is added, return the reference to the  node.

Note: The  argument is null for an empty list.

Input Format

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

Output Format

Return a reference to the  node of the linked list.

Sample Input

STDIN   Function
-----   --------
4       T = 4
2       first data = 2
3
4
1       fourth data = 1

Sample Output

2 3 4 1

Explanation

, so your method will insert  nodes into an initially empty list.
First the code returns a new node that contains the data value  as the  of the list. Then create and insert nodes , , and  at the tail of the list.

1456961238 28488bfa0d
Day 15: Linked List – Hacker Rank Solution
public class Day15LinkedList {
	static class Node{
		int data;
		Node next;
		Node(int data){
			this.data=data;
		}
	}
	 public static  Node insert(Node head,int data) {
	        //Complete this method
	        Node newNode=new Node(data);
	        if(head==null) return newNode;
	       Node node=head;
	        while(node.next!=null){
	            node=node.next;
	        }
	        node.next=newNode;
	        return head;

	    }

}

Leave a Comment

Scroll to Top