Input Format
The insert function has  parameters: a pointer to a Node named , and an integer value, .
The constructor for Node has  parameter: an integer value for the  field.
You do not need to read anything from stdin.
Output Format
Your insert function should return a reference to the  node of the linked list.
Linked list problem solution 30 days of code HackerRank

Problem solution in Java programming language.

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;
}


Second solution

public static  Node insert(Node head,int data) {
    // This is a "method-only" submission. 
    // You only need to complete this method. 
    Node current = head;
    // Setting up our new node to add
    Node newTail = new Node(data);
    newTail.next = null;
    // if the list isn't empty
    if (current != null) {
        // get to the end of the list and set the current
        // to the last element
       while (current.next != null) {
           // iterating through
            current = current.next;
        }
    } else {
        // otherwise the list is empty and we should just return our new node with the data
        return newTail;
    }
    // then add our new tail onto the end of the list
    current.next = newTail;
    return head;
}

Problem solution in JavaScript programming language.

this.insert = function(head, data) {

    // If list has no elements, return a new node:
    if(head == null) {
        return new Node(data);
    }
    // Else, iterate through the list, add node to tail, return head:
    var tmp = head;
    while(tmp.next != null){
        tmp = tmp.next;
    }
    tmp.next = new Node(data);

    return head;
};

Problem solution in the Swift programming language.

func insert(head: Node?, data: Int!) -> Node? {
    let node = Node(data: data)

    if var tmp = head {
        while tmp.next != nil {
            tmp = tmp.next!
        }

        tmp.next = node

        return head
    }

    return node
}


Problem solution in Python programming language.

class Node:
    def __init__(self,data):
        self.data = data
        self.next = None 
class Solution: 
    def display(self,head):
        current = head
        while current:
            print(current.data,end=' ')
            current = current.next

    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       

mylistSolution()
T=int(input())
head=None
for i in range(T):
    data=int(input())
    head=mylist.insert(head,data)    
mylist.display(head);