Sort by

recency

|

716 Discussions

|

  • + 0 comments

    C#

    public static Node removeDuplicates(Node head)
        {
            //Write your code here
            //My answer
            if (head.next is not null)
            {
                Node next = head.next;
                while (next.next is not null)
                {
                    Node next2 = next.next;
                    if (next.data == next2.data)
                    {
                        next.next = next2.next;
                    }
                    else
                    {
                        next = next.next;
                    }
                }
    
                next = head.next;
                if (head.data == next.data)
                {
                    head.next = next.next;
                }
    
            }
    
            return head;
        }
    
  • + 0 comments

    This will remove all duplicates, not just the adjacent ones.

    def removeDuplicates(self, head: Node | None):
        current_node = head
        s = set((current_node.data, ))
    
        while current_node.next:
            if current_node.next.data not in s:
                s.add(current_node.next.data)
                current_node = current_node.next
            else:
                current_node.next = current_node.next.next
    
        return head
    
  • + 0 comments

    Since we know from contrains:

    Constraints: The data elements of the linked list argument will always be in non-decreasing order.

    The same number should be adjacent, therefore we just need to remove (that is, skip) the adjacent duplicate numbers.

    def removeDuplicates(self,head):
        #Write your code here
        if head != None and head.next != None:
            cur = head
            new_node = cur
            new_data = cur.data
            cur = cur.next
            while cur != None:
                # duplicate, skip
                if cur.data != new_data:
                    # new value, linked nodes and update
                    new_node.next = cur
                    new_node = cur
                    new_data = cur.data
                cur = cur.next 
            new_node.next = None
        return head
    
  • + 0 comments
    Node* removeDuplicates(Node *head)
    {
     //Write your code here
     if (nullptr == head)
      return head;
               
     Node* pNode = head;     
     while(nullptr != pNode)
     {
      if (nullptr != pNode->next && pNode->data == pNode->next->data)
      {
       Node* toDelete = pNode->next;
       pNode->next = pNode->next->next;
       delete toDelete; // to not leak memory
       continue; // for triplets or more
      }
                    
      pNode = pNode->next;
     }
                
     return head;
    }
    
  • + 0 comments
    previous = head
            while head.next:
                if head.next.data == head.data:
                    head.next = head.next.next
                else:
                    head = head.next
                
            return previous