Linked List practice

LinkedList Print in reverse order

Given a linked list. Print its elements in reverse order.

Definition of a single linked list:

// Java
class ListNode {
  int val;
  ListNode next;
  ListNode(int x) {
    val = x;
    next = null;
// C++
class ListNode
  int val;
  ListNode *next;
  ListNode(int x) : val(x), next(NULL) {}

Implement function PrintReverse that prints the elements of linked list in the reverse order.

// Java
void PrintReverse(ListNode head)
// C++
void PrintReverse(ListNode *head)



Function PrintReverse must print in one line elements of a linked list in the reverse order: 3 2 1.

Time limit 1 second
Memory limit 128 MiB
Author Mykhailo Medvediev