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)
Function PrintReverse must print in one line elements of a linked list in the reverse order: 3 2 1.