Sort by

recency

|

993 Discussions

|

  • + 0 comments

    c++ solution using stack

    void reversePrint(SinglyLinkedListNode* llist) {
        std::stack<int> stack;
        SinglyLinkedListNode* node = llist;
        while (node != NULL) {
            stack.push(node->data);
            node = node->next;
        }
        while (!stack.empty()) {
            printf("%d\n", stack.top());
            stack.pop();
        }
    }
    
  • + 0 comments

    Java Solution Using Tail Recursion

    public static void reversePrint(SinglyLinkedListNode llist) {
            SinglyLinkedListNode current = llist;
    
            if(current != null){
                    reversePrint(current.next);
                    System.out.println(current.data);
            }
    }
    

    }

  • + 0 comments

    Python 3 solution, not the most efficient:

    def reversePrint(llist):
        vals = []
        while llist:
            vals.append(llist.data)
            llist = llist.next
        if vals:    
            reversed_vals = reversed(vals)
        for t in reversed_vals:
            print(t)
    
  • + 1 comment

    To the Hackerrank Team!!!! Please fix the C# variants for SingleLinkedList problems!!! Almost all of them give compiler errors. The solutions work (adapted) in java but they won't compile in C# because of the input parsing code. Thank you!

  • + 0 comments

    My Java solution:

    public static void reversePrint(SinglyLinkedListNode llist) {
            SinglyLinkedListNode reverseList = null;
            SinglyLinkedListNode current = llist;
            
            while (current != null) {
                SinglyLinkedListNode temp = new SinglyLinkedListNode(current.data);
                temp.next = reverseList;
                reverseList = temp;
                current = current.next;
            }
            
            while (reverseList != null) {
                System.out.println(reverseList.data);
                reverseList = reverseList.next;
            }
        }