Yahoo Interview Question

how to print a singly linked list backwards

Interview Answers

Anonymous

Jul 17, 2014

void reverseList(Node head) { //Base case if(head == null) return; reverseList(head.next); System.out.print(head.data); }

1

Anonymous

Sep 5, 2014

Populate the content of the linked list onto a stack. Pop each element of stack and print it.

1

Anonymous

Oct 5, 2014

Yeap , both the above answers are almost the same. The recursive call works in the stack way. Whenever a recursive call is made, the current data are pushed into a stack and when the recursion recoils, data is popped and processed accordingly from the stack.