Sort by

recency

|

696 Discussions

|

  • + 0 comments

    PSA: DO NOT USE KOTLIN

    You cannot pass using Kotlin due to this issue, which still remains today.

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

    my code in go

    func reverse(llist *DoublyLinkedListNode) *DoublyLinkedListNode {
        var last *DoublyLinkedListNode
        for llist != nil {        
            next := llist.next
            
            dumb := llist.prev
            llist.prev = llist.next
            llist.next = dumb
            last = llist
            llist = next
        }
        
        return last
    }
    
  • + 0 comments

    reverse in place:

    DoublyLinkedListNode* reverse(DoublyLinkedListNode* llist) {
        DoublyLinkedListNode* cur = llist;
        DoublyLinkedListNode* rev = NULL;
        
        while(cur) {
            DoublyLinkedListNode* next = cur->next;
            cur->next = rev;
            rev = cur;
            cur = next;
            rev->prev = cur;
        }
        
        return rev;
    }
    
  • + 0 comments

    this is my solution def reverse(llist): # Write your code here current=llist prev_node=None while current: current.prev,current.next = current.next,current.prev prev_node = current current = current.prev

    return prev_node