## Algorithm

Problem Name: 30 days of code - Day 15: Linked List

Objective
Today we will work with a Linked List. Check out the Tutorial tab for learning materials and an instructional video.

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 the 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, that must be added to the end of the list as a new Node object.

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


## Code Examples

### #1 Code Example with C Programming

Code - C Programming


#include <stdlib.h>
#include <stdio.h>
typedef struct Node
{
int data;
struct Node* next;
}Node;

{
Node * nod=
nod=(Node *)malloc(sizeof(Node));
nod->data=data;
Node *p;
while(p->next!=NULL)
p=p->next;
p->next=nod;
}

{
while(start)
{
printf("%d ",start->data);
start=start->next;
}
}
int main()
{
int T,data;
scanf("%d",&T);
while(T-->0){
scanf("%d",&data);
}

}

Copy The Code &

### #2 Code Example with C++ Programming

Code - C++ Programming


#include <iostream>

using namespace std;

class Node {
public:
int data;
Node *next;

Node(int d) {
data = d;
next = NULL;
}
};

class Solution {
public:
Node *insert(Node *head, int data) {
//Complete this method
else {
while (curr->next) curr = curr->next;
curr->next = new Node(data);
}
}

while (start) {
cout << start->data << " ";
start = start->next;
}
}
};

int main() {
Solution mylist;
int T, data;
cin >> T;
while (T-- > 0) {
cin >> data;
}
}

Copy The Code &

### #3 Code Example with C# Programming

Code - C# Programming


using System;
class Node
{
public int data;
public Node next;
public Node(int d){
data=d;
next=null;
}

}
class Solution {

public static Node insert(Node head, int data)
{
else {
while (curr.next != null) curr = curr.next;
curr.next = new Node(data);
}
}

{
while(start!=null)
{
Console.Write(start.data+" ");
start=start.next;
}
}
static void Main(String[] args) {

while(T-->0){
}
}
}


Copy The Code &

### #4 Code Example with Java Programming

Code - Java Programming


import java.util.Scanner;

class Node {
int data;
Node next;

Node(int d) {
data = d;
next = null;
}
}

class Solution {
public static Node insert(Node head, int data) {
else {
while (curr.next != null) curr = curr.next;
curr.next = new Node(data);
}
}

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);
int N = sc.nextInt();

while (N-- > 0) {
int ele = sc.nextInt();
}
sc.close();
}
}

Copy The Code &

### #5 Code Example with Javascript Programming

Code - Javascript Programming


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(){

this.insert = (head, data) => {
let node = new Node(data);

// No list, this is the first node
return node;
} else {
// Traverse the list using each node's next property
while (current.next) {
current = current.next;
}

// Append new node to tail
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++){
}
}

Copy The Code &

### #6 Code Example with Python Programming

Code - Python Programming


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

else:
while curr.next:
curr = curr.next
curr.next = Node(data)