Sort by

recency

|

985 Discussions

|

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

    here's my solution in C language

    void reversePrint(SinglyLinkedListNode* llist) { SinglyLinkedListNode *pre = NULL; SinglyLinkedListNode *next = NULL; SinglyLinkedListNode *current = llist;

    while(current != NULL){
        next = current -> next;
        current -> next = pre;
        pre = current;
        current = next;
    }
    llist = pre;
    while(llist != NULL){
        printf("%d\n", llist->data);
        llist = llist->next;
    }
    

    }

  • + 0 comments

    public static void reversePrint(SinglyLinkedListNode llist) { // Write your code here if (llist == null) return; // Base case for recursion

    reversePrint(llist.next); // Recursive call to the next node
    
    System.out.println(llist.data); // Print the current node after returning from recursion
    
    }
    
  • + 0 comments

    Kotlin:

    fun reversePrint(llist: SinglyLinkedListNode?): Unit {
        val list=ArrayList<Int>()
        var current=llist
        while(current!=null){
            list.add(current.data)
            current=current.next
        }
        list.reverse()
        list.forEach{
            println(it)
        }
    }
    
  • + 0 comments

    This is the perfect way to understand recursive

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