Sort by

recency

|

724 Discussions

|

  • + 0 comments

    Here is a general solution because I didn't read the entire specification 🤡

        def removeDuplicates(self,head):
            head_data = head.data
            
            result_head = Node(head_data)         
            previous_result_node = result_head
            
            elements = list()  
            elements.append(head_data)
            
            node = head.next        
            while node is not None:  
                node_data = node.data          
                if node_data not in elements:                
                    result_node = Node(node_data)              
                    previous_result_node.next = result_node
                    previous_result_node = result_node
                                   
                    elements.append(node_data)
                    
                node = node.next
            
            return result_head
    
  • + 1 comment

    Python 3. My janky cringy solution

    def removeDuplicates(self,head):
            cur= head
            while cur and cur.next:
                while cur.next and cur.data == cur.next.data:
                    cur.next = cur.next.next
                cur = cur.next
    
        return head
    
  • + 0 comments
    public static Node removeDuplicates(Node head) {
      //Write your code here
        Node start = head;
        while(head != null && head.next != null) {
            if (head.data == head.next.data) {
                head.next = head.next.next;
            } else {
                head = head.next;
            }
        }
        return start;
    }
    
  • + 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