# Day 15: Linked List (30 Days of Code)

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: The head argument is null for an empty list.

Input Format

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

Output Format

Sample Input

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

Sample Output

``2 3 4 1``

Explanation

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

Solutions:

1)Javascript:

``````process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
input_stdin += data;
});

process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
return input_stdin_array[input_currentline++];
}
function Node(data){
this.data=data;
this.next=null;
}
function Solution(){

//complete this method
var node = new Node(data);
}
while(current.next != null){
current = current.next;
}
current.next = node;
};

while(start){
process.stdout.write(start.data+" ");
start=start.next;
}
};
}
function main(){
var mylist=new Solution();
for(i=0;i<T;i++){
}
}       ``````

2)Java

``````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) {
//Complete this method
Node node = new Node(data);
}
while(current.next != null){
current = current.next;
}
current.next = node;
}

public static void display(Node head) {
while(start != null) {
System.out.print(start.data + " ");
start = start.next;
}
}

public static void main(String args[]) {
Scanner sc = new Scanner(System.in);