# Linked list problem solution 30 days of code HackerRank

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.

### Problem solution in Java programming language.

public static  Node insert(Node head,int data){
// if list has no elements, return a new node
return new Node(data);
}

// else iterate through list, add node to tail, and return head
while(tmp.next != null){
tmp = tmp.next;
}
tmp.next = new Node(data);

}

#### Second solution

public static  Node insert(Node head,int data) {
// This is a "method-only" submission.
// You only need to complete this method.
// 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;
}

### Problem solution in JavaScript programming language.

this.insert = function(head, data) {

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

};

### 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 node
}

### Problem solution in Python programming language.

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

temp = Node(data)
while current.next is not None:
current = current.next
current.next = temp