Inserting a Node Into a Sorted Doubly Linked List

Sort by

recency

|

31 Discussions

|

  • + 0 comments

    yet again the C# base code is borked - sigh.

  • + 0 comments

    My answer in Python:

    def sortedInsert(llist, data):
        node = llist
        dummy = DoublyLinkedListNode(0)
        dummy.next = node
        prev = dummy
        
        data = DoublyLinkedListNode(data)
        
        while node:
            if node.data >= data.data:
                break
            prev = node
            node = node.next
        
        prev.next = data
        data.prev = prev
        data.next = node
        if node:
            node.prev = data
        
        return dummy.next
    
  • + 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