Sort by

recency

|

900 Discussions

|

  • + 0 comments

    Here is my c++ solution, you can watch video explanation here : https://youtu.be/F4nGusqPIu0

    SinglyLinkedListNode* reverse(SinglyLinkedListNode* llist) {
        SinglyLinkedListNode* reverse = nullptr;
        while(llist != nullptr){
            SinglyLinkedListNode* newNode = new SinglyLinkedListNode(llist->data);
            newNode ->next = reverse;
            reverse = newNode;
            llist = llist->next;
        }
        return reverse;
    }
    
  • + 0 comments

    recursive python solution:

    def reverse(llist):
        if llist == None or llist.next == None:
            return llist
            
        new_head = reverse(llist.next)
        llist.next.next = llist
        llist.next = None
        
        return new_head
    
  • + 0 comments

    JS Recusrive solution:

    function reverse(llist) {
        if (!llist || !llist.next) {
         return llist;
       }
    
       let newHead = reverse(llist.next);
       llist.next.next = llist;
       llist.next = null;
     
       return newHead;
    }
    
  • + 0 comments
    SinglyLinkedListNode* reverse(SinglyLinkedListNode* llist) {
        
        SinglyLinkedListNode* temp=llist,*curr=llist->next,*prev=NULL;
        while(temp->next!=NULL){
        temp->next=prev;
            prev=temp;
            temp=curr;
            curr=curr->next;
        }
        temp->next=prev;
        llist=temp;
        return llist;           
    }
    
  • + 0 comments
    SinglyLinkedListNode* reverse(SinglyLinkedListNode* llist) {
        //if(llist==NULL)
        
        SinglyLinkedListNode* temp=llist,*curr=llist->next,*prev=NULL;
        while(temp->next!=NULL){
        temp->next=prev;
            prev=temp;
            temp=curr;
            curr=curr->next;
        }
        temp->next=prev;
        llist=temp;
        return llist;           
    }