import * as fs from 'fs';

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

let inputString: any = '';
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();
});

function readLine() {
    return inputString[currentLine++];
}

/*
 * For your reference:
 *
 * SinglyLinkedListNode {
 *     int data;
 *     SinglyLinkedListNode next;
 * }
 *
 */
export function insertNodeAtHead(head: SinglyLinkedListNode, data: number) {
    const newNode = new SinglyLinkedListNode(data);

    newNode.next = head;

    return newNode;
}

class SinglyLinkedListNode {
    public data: number;
    public next: SinglyLinkedListNode;

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

class SinglyLinkedList {
    public head: SinglyLinkedListNode;
    public tail: SinglyLinkedListNode;

    constructor() {
        this.head = null;
        this.tail = null;
    }
};

function printSinglyLinkedList(node, sep, ws) {
    while (node != null) {
        ws.write(String(node.data));

        node = node.next;

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

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

    const llistCount = parseInt(readLine(), 10);

    let llist = new SinglyLinkedList();

    for (let i = 0; i < llistCount; i++) {
        const llistItem = parseInt(readLine(), 10);
        const llist_head = insertNodeAtHead(llist.head, llistItem);
        llist.head = llist_head;
    }



    printSinglyLinkedList(llist.head, '\n', ws);
    ws.write('\n');

    ws.end();
}
Create implementation of insert node at head.
Create implementation of insert node at head.
