Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. /*
  2. Question
  3. Print reverse LinkedList
  4. SHOW SOLUTION
  5. SUGGEST EDIT
  6. Level EASY
  7. Print a given linked list in reverse order. You need to print the tail first and head last. You can’t change any pointer in the linked list, just print it in reverse order.
  8. Input format : Linked List elements (separated by space and terminated by -1)
  9. Output format : Linked List elements in reverse order (separated by space)
  10. Sample Input 1 :
  11. 1 2 3 4 5 -1
  12. Sample Output 1 :
  13. 5 4 3 2 1
  14. Sample Input 2 :
  15. 1 2 3 -1
  16. Sample Output 2 :
  17. 3 2 1
  18. */
  19. /*
  20. class LinkedListNode<T> {
  21. public T data;
  22. public LinkedListNode<T> next;
  23.  
  24. public LinkedListNode(T data) {
  25. this.setData(data);
  26. this.next = null;
  27. }
  28.  
  29. public T getData() {
  30. return data;
  31. }
  32.  
  33. public void setData(T data) {
  34. this.data = data;
  35. }
  36.  
  37. }
  38. * */
  39. public class Solution {
  40. public static void printReverseRecursive(LinkedListNode<Integer> head) {
  41. if (head == null) {
  42. return;
  43. }
  44. printReverseRecursive(head.next);
  45. System.out.print(head.data + " ");//PRINTING WHILE FALLING DOWN THE STACK
  46.  
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement