Sort by

recency

|

990 Discussions

|

  • + 0 comments

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

    Here is my c++ solution, you can watch the vidéo explanation here : https://youtu.be/WO7Uz7sQML4

    void reversePrint(SinglyLinkedListNode* llist) {
        if(llist == nullptr) return;
        reversePrint(llist->next);
        cout << llist->data << endl;
    }