## Algorithm

Problem Name: Data Structures - Print the Elements of a Linked List

In this HackerRank in Data Structures - Print the Elements of a Linked List

This is an to practice traversing a linked list. Given a pointer to the head node of a linked list, print each node's data element, one per line. If the head pointer is null (indicating the list is empty), there is nothing to print.

Function Description

Complete the printLinkedList function in the editor below.

Print

For each node, print its data value on a new line (console.log in Javascript).

Input Format

The first line of input contains n , the number of elements in the linked list.
The next n lines contain one element each, the data values for each node.

Note: Do not read any input from stdin/console. Complete the printLinkedList function in the editor below.

Constraints

• 1 <= n <= 1000
• 1 <= list[i] <= 1000, where list[i] is the i**th element of the linked list.

Sample Input

``````2
16
13
``````

Sample Output

``````16
13
``````

Explanation

There are two elements in the linked list. They are represented as 16 -> 13 -> NULL. So, the printLinkedList function should print 16 and 13 each on a new line.

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

} else {
}

}

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

free(temp);
}
}

// Complete the printLinkedList function below.

/*
*
*     int data;
* };
*
*/
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
}

int main()
{
llist->tail = NULL;

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

}

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

size_t new_length = alloc_length << 1;
data = realloc(data, new_length);

if (!data) { break; }

alloc_length = new_length;
}

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

data = realloc(data, data_length);

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:

this-> tail = nullptr;
}

void insert_node(int node_data) {

} else {
this-> tail-> next = node;
}

this->tail = node;
}
};

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

free(temp);
}
}

// Complete the printLinkedList function below.

/*
*
*     int data;
* };
*
*/

return;
}

}

int main()
{

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');

llist->insert_node(llist_item);
}

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

### #3 Code Example with Python Programming

```Code - Python Programming```

``````
#!/bin/python3

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):
self.tail = None

def insert_node(self, node_data):

else:
self.tail.next = node

self.tail = node

# Complete the printLinkedList function below.

#
#
#     int data
#
#
while curr:
print(curr.data)
curr = curr.next

if __name__ == '__main__':
llist_count = int(input())

for _ in range(llist_count):
llist_item = int(input())
llist.insert_node(llist_item)

``````
Copy The Code &

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

this.tail = null;
}

public void insertNode(int nodeData) {

} else {
this.tail.next = node;
}

this.tail = node;
}
}

// Complete the printLinkedList function below.

/*
*
*     int data;
* }
*
*/
}

}

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

public static void main(String[] args) {

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])?");

llist.insertNode(llistItem);
}