## Algorithm

Problem Name: Data Structures - Insert a Node at the Tail of a Linked List

In this HackerRank in Data Structures - Insert a Node at the Tail of a Linked List in List solutions

This challenge is part of a tutorial track by MyCodeSchool and is accompanied by a video lesson.

You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty.

Function Description

Complete the insertNodeAtTail function in the editor below.

insertNodeAtTail has the following parameters:

• int data: the data value for the node to insert

Returns

Input Format

The first line contains an integer n, the number of elements in the linked list.
The next n lines contain an integer each, the value that needs to be inserted at tail.

Constraints

• 1 <= n <= 1000
• 1 <= list[i] <= 1000

Sample Input

STDIN Function ----- -------- 5 size of linked list n = 5 141 linked list data values 141..474 302 164 530 474

Sample Output

``````141
302
164
530
474
``````

Explanation

First the linked list is NULL. After inserting 141, the list is 141 -> NULL.
After inserting 302, the list is 141 -> 302 -> NULL.
After inserting 164, the list is 141 -> 302 -> 164 -> NULL.
After inserting 530, the list is 141 -> 302 -> 164 -> 530 -> NULL. After inserting 474, the list is 141 -> 302 -> 164 -> 530 -> 474 -> NULL, which is the final list.

## Code Examples

### #1 Code Example with C Programming

```Code - C Programming```

``````
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int data;
};

};

node->data = node_data;
node->next = NULL;

return node;
}

while (node) {
fprintf(fptr, "%d", node->data);

node = node->next;

if (node) {
fprintf(fptr, "%s", sep);
}
}
}

while (node) {
node = node->next;

free(temp);
}
}

// Complete the insertNodeAtTail function below.

}

while (node->next != NULL) {
node = node->next;
}
}

int main()
{
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");

char* llist_count_endptr;
int llist_count = strtol(llist_count_str, &llist_count_endptr, 10);

if (llist_count_endptr == llist_count_str || *llist_count_endptr != '\0') { exit(EXIT_FAILURE); }

for (int i = 0; i  <  llist_count; i++) {
char* llist_item_endptr;
int llist_item = strtol(llist_item_str, &llist_item_endptr, 10);

if (llist_item_endptr == llist_item_str || *llist_item_endptr != '\0') { exit(EXIT_FAILURE); }

}

char *sep = "\n";

fprintf(fptr, "\n");

fclose(fptr);

return 0;
}

size_t alloc_length = 1024;
size_t data_length = 0;
char* data = malloc(alloc_length);

while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);

if (!line) {
break;
}

data_length += strlen(cursor);

if (data_length  <  alloc_length - 1 || data[data_length - 1] == '\n') {
break;
}

alloc_length <<= 1;

data = realloc(data, alloc_length);

if (!line) {
break;
}
}

if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';

data = realloc(data, data_length);
} else {
data = realloc(data, data_length + 1);

data[data_length] = '\0';
}

return data;
}
``````
Copy The Code &

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

```Code - C++ Programming```

``````
#include <bits/stdc++.h>

using namespace std;

public:
int data;

this->data = node_data;
this->next = nullptr;
}
};

public:

}

};

while (node) {
fout << node->data;

node = node->next;

if (node) {
fout << sep;
}
}
}

while (node) {
node = node->next;

free(temp);
}
}

}
else{
}
}
}

int main()
{
ofstream fout(getenv("OUTPUT_PATH"));

int llist_count;
cin >> llist_count;
cin.ignore(numeric_limits < streamsize>::max(), '\n');

for (int i = 0; i  <  llist_count; i++) {
int llist_item;
cin >> llist_item;
cin.ignore(numeric_limits < streamsize>::max(), '\n');

}

fout << "\n";

fout.close();

return 0;
}
``````
Copy The Code &

### #3 Code Example with Java Programming

```Code - Java Programming```

``````
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

public int data;

this.data = nodeData;
this.next = null;
}
}

}

}

public static void printSinglyLinkedList(SinglyLinkedListNode node, String sep, BufferedWriter bufferedWriter) throws IOException {
while (node != null) {
bufferedWriter.write(String.valueOf(node.data));

node = node.next;

if (node != null) {
bufferedWriter.write(sep);
}
}
}

if(temp == null){
}
while(temp.next != null){
temp = temp.next;
}
}

private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

int llistCount = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

for (int i = 0; i < llistCount; i++) {
int llistItem = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

}

bufferedWriter.newLine();

bufferedWriter.close();

scanner.close(>;
}
}
``````
Copy The Code &

### #4 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
inputString += inputStdin;
});

process.stdin.on('end', function() {
inputString = inputString.replace(/\s*\$/, '')
.split('\n')
.map(str => str.replace(/\s*\$/, ''));

main();
});

return inputString[currentLine++];
}

constructor(nodeData) {
this.data = nodeData;
this.next = null;
}
};

constructor() {
}

};

while (node != null) {
ws.write(String(node.data));

node = node.next;

if (node != null) {
ws.write(sep);
}
}
}

}else {
//find tail
while(curr.next){
next = curr.next;
curr = next;
}
}
}

function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

for (let i = 0; i < llistCount; i++) {
}

ws.write('\n');

ws.end(>;
}
``````
Copy The Code &

### #5 Code Example with Python Programming

```Code - Python Programming```

``````
import math
import os
import random
import re
import sys

def __init__(self, node_data):
self.data = node_data
self.next = None

def __init__(self):

while node:
fptr.write(str(node.data))

node = node.next

if node:
fptr.write(sep)

while cur.next != None:
cur = cur.next

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

llist_count = int(input())

for i in range(llist_count):
llist_item = int(input())