Inserting a Node Into a Sorted Doubly Linked List

Sort by

recency

|

29 Discussions

|

  • + 0 comments

    Easy to understans Solution in C++

    DoublyLinkedListNode *t = new DoublyLinkedListNode(data); 
        
        if(llist==nullptr ){
            return t; 
        }
        
        else if( llist->next==nullptr && llist->data<data){
            llist->next=t;
            t->prev=llist;
        }
        else if(llist->data >data){
            DoublyLinkedListNode *te = new DoublyLinkedListNode(llist->data);
            DoublyLinkedListNode* Next=llist->next;
            llist->data=data;
            te->next=llist->next;
            Next->prev=te;
            te->prev=llist;
            llist->next=te;
            return llist;
        }
        DoublyLinkedListNode* temp =llist;
        while(temp->next!=nullptr){
            temp=temp->next;
        }
        if(temp->data<data){
            temp->next=t;
            return llist;
        }
        temp=llist;
        while(temp->data < data){
            temp = temp->next;
        }
        DoublyLinkedListNode* temp2 =temp->prev;
        cout<<temp2->data;
        temp2->next=t;
        t->prev=temp2;    
        t->next=temp;
        temp->prev=t;
        return llist;
    
  • + 0 comments

    Java 15. In java 7 and 8, you may encounter static problem so code in java 15

    DoublyLinkedListNode n = new DoublyLinkedListNode(data);

        if (llist == null) {
            return n;
        }
    
        // Case when the new node needs to be inserted at the beginning
        if (data < llist.data) {
            n.next = llist;
            llist.prev = n;
            return n;
        }
    
        DoublyLinkedListNode current = llist;
        while (current.next != null && current.next.data < data) {
            current = current.next;
        }
    
        // Insert the new node
        n.next = current.next;
        if (current.next != null) {
            current.next.prev = n;
        }
        current.next = n;
        n.prev = current;
    
        return llist;
    
  • + 0 comments

    The test harness for Kotlin has a missing newline after each test case, since you can't edit the test harness adding a global var alreadyCalled = false and then add this into your sortedInsert will fix the broken tests:

        if (alreadyCalled) {
            println()
        }
        
        alreadyCalled = true
    
  • + 0 comments

    Java 15

     public static DoublyLinkedListNode sortedInsert(DoublyLinkedListNode llist, int data) {
            DoublyLinkedListNode newNode = new DoublyLinkedListNode(data);
    
            if (llist == null) {
                return newNode; 
            }
    
            if (data <= llist.data) {
                newNode.next = llist;
                llist.prev = newNode;
                return newNode;
            }
    
            DoublyLinkedListNode current = llist;
    
            while (current.next != null && current.next.data < data) {
                current = current.next;
            }
    
            newNode.next = current.next;
            if (current.next != null) {
                current.next.prev = newNode;
            }
            current.next = newNode;
            newNode.prev = current;
    
            return llist;
        }
    
  • + 0 comments

    why java 8 are giving static error