Reverse a doubly linked list

  • + 0 comments

    Solution in C:

    DoublyLinkedListNode* reverse(DoublyLinkedListNode* llist) {
        struct DoublyLinkedListNode* curr = *(&llist);
        struct DoublyLinkedListNode* prev = NULL;
        struct DoublyLinkedListNode* next;
    
        while (curr != NULL)
        {
            next = curr->next;
            curr->next = prev;
            curr->prev = next;
            prev = curr;
            curr =  next;
        }
    
        *(&llist) = prev;
        
        return *(&llist);
    }