• + 5 comments

    Recursion never felt so good :D

    • + 3 comments

      That conditional is a bit ugly, why not

      if(head == null) return;
      ReversePrint(head.next);
      System.out.println(head.data);
      
      // OR
      
      if (head != null) {
         ReversePrint(head.next);
         System.out.println(head.data); 
      }
      
      • + 1 comment

        nice

        • + 1 comment
          [deleted]
          • + 0 comments

            here is problem solution in python java c++ and c programming. https://solution.programmingoneonone.com/2020/07/hackerrank-print-in-reverse-solution.html

      • + 1 comment

        It worked. but it is bit confusing since there is already a "printSinglyLinkedList(...)" function so I was assuming we need to use this function to print elements.

        • + 0 comments

          here is problem solution in python java c++ and c programming. https://solution.programmingoneonone.com/2020/07/hackerrank-print-in-reverse-solution.html

      • + 3 comments

        Even shorter:

        static void reversePrint(SinglyLinkedListNode head) {
                if(head.next!=null) reversePrint(head.next);
                System.out.println(head.data);
        
            }
        
        • + 0 comments

          This will throw a NullPointerException when given head is null. So you might want to handle that.

    • + 1 comment

      So true :)

      • + 0 comments

        here is problem solution in java python c++ c and javascript programming. https://programs.programmingoneonone.com/2021/05/hackerrank-print-in-reverse-solution.html

    • + 0 comments

      So true. :D

    • + 1 comment

      void ReversePrint(Node *head) { // This is a "method-only" submission. // You only need to complete this method. if(head){ ReversePrint(head->next); printf("%d\n",head->data); } }

      • + 1 comment

        could you please explain your solution? Thanks!!

    • + 0 comments

      My only conern with this is that a LinkedList could be quite long, and the recursion depth limit is usually only around 5000, which really isn't that deep.