Sort by

recency

|

721 Discussions

|

  • + 0 comments

    C#

      public static Node removeDuplicates(Node head){
        
        var current = head;
        
        while(current.next is not null)
        {
            if(current.data == current.next.data)
            {
                current.next = current.next.next;
            }
            else current = current.next;
        }
        
        return head;
      }
    
  • + 0 comments

    Python3 solution

        def removeDuplicates(self,head):
            slow = fast = head
            while fast.next:
                fast = fast.next
                
                if fast.data == slow.data:
                    continue
                
                slow.next = slow = fast
            
            slow.next = None        
            return head
    
  • + 0 comments

    Simplest Py3 solution:

    def removeDuplicates(self,head):
            if head is None:
                return head
                
            start = head
            curr = head
            
            while curr.next is not None:
                if curr.next.data == curr.data:
                    curr.next = curr.next.next
                else:
                    curr = curr.next
                    
            return start
    
  • + 0 comments

    Java

       public static Node removeDuplicates(Node head) {
             if(head==null||head.next==null){
        return head;
      }
    
      List<Integer> list = new ArrayList<>();
      Node current = head;
      while(current.next!=null){
        list.add(current.data); 
         if(list.contains(current.next.data)){
            // skip the next node
            current.next = current.next.next;                  
            }else {
                current = current.next;
            }         
      }
      return head;
    }
    
  • + 0 comments

    My solution in Python 3 using dictionary

    def removeDuplicates(self,head):
        d = {}
        cur = n = head
    
        while n is not None:
            if n.data not in d.keys():
                d[n.data] = True
                cur = n
            else:
                cur.next = n.next
            n = n.next
        return head