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: 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
Return a reference to the head 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
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();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
function Node(data){
this.data=data;
this.next=null;
}
function Solution(){
this.insert=function(head,data){
//complete this method
var node = new Node(data);
if(head == null){
head = node;
return head;
}
var current = head;
while(current.next != null){
current = current.next;
}
current.next = node;
return head;
};
this.display=function(head){
var start=head;
while(start){
process.stdout.write(start.data+" ");
start=start.next;
}
};
}
function main(){
var T=parseInt(readLine());
var head=null;
var mylist=new Solution();
for(i=0;i<T;i++){
var data=parseInt(readLine());
head=mylist.insert(head,data);
}
mylist.display(head);
}
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);
if(head == null){
head = node;
return head;
}
Node current = head;
while(current.next != null){
current = current.next;
}
current.next = node;
return head;
}
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();
}
}