e-olymp
favorite We need a little bit of your help to keep things running, click on this banner to learn more
Competitions

class LinkedList

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
{
public:
  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)

Example

prb9898.gif

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