Delete duplicate-value nodes from a sorted linked list

Sort by

recency

|

913 Discussions

|

  • + 0 comments

    JAVA

     public static SinglyLinkedListNode removeDuplicates(SinglyLinkedListNode llist) {
        // Write your code here
            if(llist.next == null){
                return llist;
            }
            SinglyLinkedListNode first = llist;
            SinglyLinkedListNode second = llist.next;
            
            while(first.next != null){
                if(first.data == second.data){
                    if(second.next != null){
                        second = second.next;           
                    }else{
                        first.next = null;
                    }
                }else{
                    first.next = second;
                     if(second.next != null){
                        second = second.next;
                     }
                    first = first.next;
                }
            }
            return llist;
        }
    
  • + 0 comments

    PYTHON

    def removeDuplicates(llist):

        # Write your code here
    current=llist
    
    while current:
        if current.next and current.data == current.next.data:
            current.next = current.next.next
        else:
            current = current.next
    
    return llist
    
  • + 0 comments
     static SinglyLinkedListNode removeDuplicates(SinglyLinkedListNode llist) {
        // Write your code here 
        SinglyLinkedListNode newNode = new SinglyLinkedListNode(0);
        SinglyLinkedListNode tail = newNode;
        while(llist != null)
        {
            if(tail.data == llist.data)
            {
                llist = llist.next ;
            }
            else{
                tail.next = llist;
                tail = llist;
                llist = llist.next;
            }
        }
        tail.next = null;
        return newNode.next;
    
        }
    
  • + 0 comments
    SinglyLinkedListNode* removeDuplicates(SinglyLinkedListNode* llist) {
        SinglyLinkedListNode *current = llist, *prev = NULL ;
        
        while(current->next != 0){
            prev = current;
            current = current->next;
            if(prev->data == current->data){
                prev->next = current->next;
                current = prev;
            }
        }
        
        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

    Source https://statesmd.com/specialties/