• + 0 comments

    My Java solution with linear time complexity and constant space complexity:

     public static DoublyLinkedListNode reverse(DoublyLinkedListNode llist) {
            //return llist if its null, indicating the llist is empty
            if(llist == null) return llist;
            
            //use a curr ptr
            DoublyLinkedListNode curr = llist;
            
            //iterate thru the llist
            while(curr != null){
                //store curr nxt in a temp var
                DoublyLinkedListNode temp = curr.next;
                curr.next = curr.prev;
                curr.prev = temp;
                
                if(temp == null) break;
                else curr = temp;
            }
            return curr;        
        }