Delete duplicate-value nodes from a sorted linked list

Sort by

recency

|

907 Discussions

|

  • + 0 comments
        SinglyLinkedListNode* temp1;
        temp1=llist;
        
        while(temp1!=nullptr&&temp1->next!=nullptr)
        {
            if(temp1->data==temp1->next->data)
            {
                temp1->next=temp1->next->next;
            }else
            {
                temp1=temp1->next;
            }
                
        }
        
        return llist;
    }
    
    
    
  • + 0 comments

    Here, my solution:

    SinglyLinkedListNode* removeDuplicates(SinglyLinkedListNode* llist) {
        SinglyLinkedListNode* prev;
        SinglyLinkedListNode* curr = llist;
        
        while(curr != NULL) {
            prev = curr;
            curr = curr->next;
            
            while(curr != NULL && curr->data == prev->data){
                prev->next = curr->next;
                curr = curr->next;
            }
        }
        
        return llist;
    }
    
  • + 0 comments

    Hi There i am try to make a search box just link any one who can make me code like this .... thanks

  • + 0 comments

    My solution in C#: easiest solution

    static SinglyLinkedListNode removeDuplicates(SinglyLinkedListNode llist) { SinglyLinkedListNode currNode = llist;

        while(currNode !=null && currNode.next!= null)
        {
            if(currNode.data==currNode.next.data)
            {
                currNode.next = currNode.next.next;
            }
            else 
            currNode = currNode.next;
    
        }
        return llist;
    }
    
  • + 1 comment
    let node = llist
    
    while(node != null) {
        if(node.next== null) {
            return llist
        }
    
        let firstNode = node.data
        let secondNode = node.next.data   
    
        if(firstNode != secondNode) {
            node = node.next
        }         
    
        if(firstNode == secondNode) {
            node.next = node.next.next
        }
    }
    

    return llist