Day 15 linked list hackerrank solution in java

Type Here to Get Search Results !

HomeHackerRank 30 Days Of CodeDay 15: HackerRank 30 Days Of Code Solution By CodingHumans | Linked List |


Day 15: Linked List  Hey Coders today we're working with Linked Lists. Linked List A singly linked list is a data structure having a list of elements where each element has a reference pointing to the next element in the list. Its elements are generally referred to as nodes; each node has a data field containing a data value and a next field pointing to the next element in the list [or null if it is the last element in the list]. The diagram given below depicts a linked list of length :


Aim A Node class is provided for you in the editor. A Node object has an integer data field, data, and a Node instance pointer, next , pointing to another node [i.e.: the next node in a list]. A Node insert function is also declared in your editor. It has two parameters: a pointer,head , pointing to the first node of a linked list, and an integer data value 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 data as the Node constructor argument] and inserts it at the tail of the linked list referenced by the head  parameter. Once the new node is added, return the reference to the head  node. Note: If the head argument passed to the insert function is null, then the initial list is empty.

Input Format

The insert function has 2 parameters: a pointer to a Node named head , and an integer value, . The constructor for Node has 1 parameter: an integer value for the data field. You do not need to read anything from stdin.

Output Format

Your insert function should return a reference to the head node of the linked list.

Sample Input

The following input is handled for you by the locked code in the editor: The first line contains T, the number of test cases. The T subsequent lines of test cases each contain an integer to be inserted at the list's tail. 4 2 3 4 1

Sample Output

The locked code in your editor prints the ordered data values for each element in your list as a single line of space-separated integers: 2 3 4 1

Explanation

T=4, so the locked code in the editor will be inserting 4 nodes. The list is initially empty, so head  is null; accounting for this, our code returns a new node containing the data value 2  as the head of our list. We then create and insert nodes 3, 4, and 1  at the tail of our list. The resulting list returned by the last call to insert is  [2,3,4,1], so the printed output is 2 3 4 1.

Recommended: Please try your approach on your integrated development environment [IDE] first, before moving on to the solution.


Few words from CodingHumans : Don't Just copy paste the solution, try to analyze the problem and solve it without looking by taking the the solution as a hint or a reference . Your understanding of the solution matters.

import java.io.*; import java.util.*; class Node { int data; Node next; Node[int d] { data = d; next = null; } } class Solution { public static Node insert[Node head,int data] { if[head==null]{ return new Node [data]; } else if[head.next==null]{ head.next=new Node [data]; } else{ insert[head.next,data]; } return head; //Complete this method } public static void display[Node head] { Node start = head; while[start != null] { System.out.print[start.data + " "]; start = start.next; } } public static void main[String args[]] { Scanner sc = new Scanner[System.in]; Node head = null; int N = sc.nextInt[]; while[N-- > 0] { int ele = sc.nextInt[]; head = insert[head,ele]; } display[head]; sc.close[]; } }

If you have any doubts regarding this problem or  need the solution in other programming languages then leave a comment down below . 

Tags


In this HackerRank Day 15 Linked List 30 days of code problem Complete the insert function in your editor so that it creates a new Node [pass data as the Node constructor argument] and inserts it at the tail of the linked list referenced by the head parameter. Once the new node is added, return the reference to the head node.  

Problem solution in Python 2 programming.

def insert[self,head,data]: #Complete this method node = Node[data] if head == None: head = node else: current = head while current.next: current = current.next current.next = node return head



def insert[self,head,data]: temp = Node[data] if head is None: head = temp return head current = head while current.next is not None: current = current.next current.next = temp return head

Problem solution in java programming.

public static Node insert[Node head,int data]{ // if list has no elements, return a new node if[head == null]{ return new Node[data]; } // else iterate through list, add node to tail, and return head Node tmp = head; while[tmp.next != null]{ tmp = tmp.next; } tmp.next = new Node[data]; return head; }

Problem solution in c++ programming.

Node* insert[Node *head,int data] { //Complete this method if[head == nullptr]{ return new Node[data]; } Node *temp = head; while[temp->next != nullptr]{ temp = temp->next; } temp->next = new Node[data]; return head; }

Problem solution in c programming.

Node* insert[Node *head,int data] { //Complete this function struct Node *ll, *it; ll=[struct Node *]malloc[sizeof[struct Node]]; ll->data=data; ll->next=NULL; if[head==NULL] { head=ll; } else { it=head; while[it->next] it=it->next; it->next=ll; } return head; }

Problem solution in Javascript programming.

this.insert=function[head,data]{ //complete this method var newNode = new Node[data]; if [head === null || typeof head === 'undefined'] { head = newNode; } else if [head.next === null] { head.next = newNode; } else { var next = head.next; while[next.next] { next = next.next } next.next = newNode; } return head; };

Video liên quan

Chủ Đề