Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. /*
  2. Question
  3. Given a linked list, reverse it iteratively.
  4. You don't need to print the elements, just reverse the LL duplicates and return the head of updated LL.
  5. Input format : Linked list elements (separated by space and terminated by -1)`
  6. Sample Input 1 :
  7. 1 2 3 4 5 -1
  8. Sample Output 1 :
  9. 5 4 3 2 1
  10. */
  11. //Solution
  12.  
  13. /*
  14. class LinkedListNode<T> {
  15. public T data;
  16. public LinkedListNode<T> next;
  17.  
  18. public LinkedListNode(T data) {
  19. this.setData(data);
  20. this.next = null;
  21. }
  22.  
  23. public T getData() {
  24. return data;
  25. }
  26.  
  27. public void setData(T data) {
  28. this.data = data;
  29. }
  30.  
  31. }
  32. * */
  33. public class Solution {
  34. public static void printReverseRecursive(LinkedListNode<Integer> head) {
  35. if (head == null) {
  36. return;
  37. }
  38. printReverseRecursive(head.next);
  39. System.out.print(head.data + " ");//PRINTING WHILE FALLING DOWN THE STACK
  40.  
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement