Interview Question

Software Engineer Interview San Jose, CA

how to print a singly linked list backwards

Answer

Interview Answer

3 Answers

1

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

Mahe on Jul 17, 2014
1

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

Anonymous on Sep 4, 2014
0

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.

Muthu on Oct 5, 2014

Add Answers or Comments

To comment on this, Sign In or Sign Up.