Sort by

recency

|

991 Discussions

|

  • + 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;
            }
        }
    
  • + 0 comments

    JavaScript Solution:

    function reversePrint(llist) {
        // Write your code here
        if (llist == null) return;
        let current = llist;
        let stack = [];
        while (current) {
            stack.push(current);
            current = current.next
        }
        while (stack.length > 0) {
            console.log(stack.pop().data);
        }
    }
    
  • + 0 comments

    My Java solution with linear time complexity and constant space complexity using recursion:

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