Sort by

recency

|

894 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

    SinglyLinkedListNode* reverse(SinglyLinkedListNode* llist) { SinglyLinkedListNode* prev = NULL; SinglyLinkedListNode* current = llist; SinglyLinkedListNode* next = NULL; while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; } return prev;

    }

  • + 0 comments

    SinglyLinkedListNode* reverse(SinglyLinkedListNode* llist) { SinglyLinkedListNode* prev = NULL; SinglyLinkedListNode* current = llist; SinglyLinkedListNode* next = NULL;

    while (current != NULL) {
        next = current->next;
        current->next = prev;
        prev = current;
        current = next;
    }
    return prev;
    

    }

  • + 0 comments
    def reverse(llist):
        reverse, stack, current = SinglyLinkedList(), [], llist
        while current:
            stack.append(current.data)
            current = current.next
        while stack:
            reverse.insert_node(stack.pop())
        return reverse.head
    
  • + 0 comments

    Easiest Solution with c#

    just remove the Class and public The final solution is:

    static SinglyLinkedListNode reverse(SinglyLinkedListNode llist)
        {
        SinglyLinkedListNode curr = llist;
        SinglyLinkedListNode prev = null;
        SinglyLinkedListNode next = null;
        
        while(curr!= null)
        {
            next = curr.next; //firstly initialising next to its exact location
            curr.next = prev;//reversing the link
            prev=curr; //will move prev by +1
    
        curr = next;//will move curr by +1  
    }
    return prev;
    }