## Algorithm

Problem Name: Data Structures - Print in Reverse

In this HackerRank in Data Structures - Print in Reverse solutions

Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything.

Example

1 -> 2 -> 3 -> NULL

Print the following:
 3 2 1 

Function Description

Complete the reversePrint function in the editor below.

reversePrint has the following parameters:

Prints

The

values of each node in the reversed list.

Input Format

The first line of input contains t, the number of test cases.

The input of each test case is as follows:

• The first line contains an integer n, the number of elements in the list.
• Each of the next n lines contains a data element for a list node.

Constraints

1 <= n <= 1000

1 <= list[i] <= 1000, where list[i] is the i**th element in the list.

Sample Input

3
5
16
12
4
2
5
3
7
3
9
5
5
1
18
3
13


Sample Output

5
2
4
12
16
9
3
7
13
3
18
1
5


Explanation

There are three test cases. There are no blank lines between test case output.

The first linked list has 5 elements: 16 -> 12 -> 2 -> 4.Printing this in reverse order produces:
5 2 4 12 16 The second linked list has 3 elements: 7 -> 3 -> 9 -> NULL. Printing this in reverse order produces:
9 3 7
The third linked list has 5 elements:

5 - > 1 -> 18 -> 3 -> NULL. Printing this in reverse order produces:
13 3 18 1 5

## 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 <sdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int data;
};

};

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

return node;
}

} else {
}

}

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

node = node->next;

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

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

free(temp);
}
}

/*
* Complete the 'reversePrint' function below.
*
* The function accepts INTEGER_SINGLY_LINKED_LIST llist as parameter.
*/

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

else{
}

}

int main()
{
char* tests_endptr;
int tests = strtol(tests_str, &tests_endptr, 10);

if (tests_endptr == tests_str || *tests_endptr != '\0') { exit(EXIT_FAILURE); }

for (int tests_itr = 0; tests_itr < tests; tests_itr++) {
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;

string ltrim(const string &);
string rtrim(const string &);

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) {
cout << node->data;

node = node->next;

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

/*
* Complete the 'reversePrint' function below.
*
* The function accepts INTEGER_SINGLY_LINKED_LIST llist as parameter.
*/

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

return;
}

}

int main()
{
string tests_temp;
getline(cin, tests_temp);

int tests = stoi(ltrim(rtrim(tests_temp)));

for (int tests_itr = 0; tests_itr < tests; tests_itr++) {

string llist_count_temp;
getline(cin, llist_count_temp);

int llist_count = stoi(ltrim(rtrim(llist_count_temp)));

for (int i = 0; i  <  llist_count; i++) {
string llist_item_temp;
getline(cin, llist_item_temp);

int llist_item = stoi(ltrim(rtrim(llist_item_temp))>;

llist->insert_node(llist_item);
}

}

return 0;
}

string ltrim(const string &str) {
string s(str);

s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun < int, int>(isspace)))
);

return s;
}

string rtrim(const string &str) {
string s(str);

s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun < int, int>(isspace))).base(),
s.end()
);

return s;
}

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

this.tail = null;
}

public void insertNode(int nodeData) {

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

this.tail = node;
}
}

while (node != null) {
System.out.print(node.data);

node = node.next;

if (node != null) {
System.out.print(sep);
}
}
}

// print list of head node

// After everything else is printed

}

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

public static void main(String[] args) {
int tests = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

for (int testsItr = 0; testsItr  <  tests; testsItr++) {

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

}

scanner.close();
}
}

Copy The Code &

### #4 Code Example with Javascript Programming

Code - Javascript Programming


'use strict';

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

let inputString = '';
let currentLine = 0;

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

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

main();
});

return inputString[currentLine++];
}

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

constructor() {
this.tail = null;
}

insertNode(nodeData) {

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

this.tail = node;
}
};

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

node = node.next;

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

/*
* Complete the 'reversePrint' function below.
*
* The function accepts INTEGER_SINGLY_LINKED_LIST llist as parameter.
*/

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

}
}

function main() {

for (let testsItr = 0; testsItr  <  tests; testsItr++) {

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