Delete duplicate-value nodes from a sorted linked list

Sort by

recency

|

921 Discussions

|

  • + 0 comments

    This C++ solution mirrors the Python logic and is clean, robust, and handles corner cases well (e.g., duplicates at the start). Memory is handled with a dummy pointer to help simplify pointer rewiring. Betbricks7 com sign up

  • + 0 comments

    Hello I tried to solve this with kotlin cause Im Software android Engineer and I feel more familiar with kotlin

    fun removeDuplicates(llist: SinglyLinkedListNode?): SinglyLinkedListNode? {
     var current = list
    
     while(current?.next != null){
     if(current.data == current.next?.data){
     current.next = current.next?.next
     }else{
     current = current.next
     }
     }
     return list
    }
    

    My logic is correct but Hacker rank has a kind of bug working with nulleables types the same logic in python works correctly why?

    def removeDuplicates(llist):
     current = llist
    
    while current.next is not None:
    if current.data == current.next.data:
    current.next = current.next.next
    else:
    current = current.next
    
    return llist
    
  • + 0 comments

    My Java solution with o(n) time complexity and o(1) space complexity:

    public static SinglyLinkedListNode removeDuplicates(SinglyLinkedListNode llist) {
            if(llist == null) return llist; //no duplicates to rmv
            
            //use two ptrs to iterate over the linked list
            SinglyLinkedListNode first = llist;
            SinglyLinkedListNode second = llist.next;
            
            while(first != null && second != null){
                //remove the second ptr if its data is equal to the first ptr
                if(first.data == second.data) first.next = second.next;
                //otherwise increase first ptr since data isnt equal
                else first = second;
                
                //increment second ptr to go through entire list
                second = second.next;
            }
            
            return llist;
        }
    
  • + 0 comments

    def removeDuplicates(llist): # Write your code here var=llist while var: if var.next and var.data == var.next.data: var.next = var.next.next else: var = var.next return llist

  • + 0 comments

    python

    def removeDuplicates(llist): # Write your code here temp=llist while temp and temp.next: if(temp.data==temp.next.data): delnode=temp.next temp.next=temp.next.next del delnode else: temp=temp.next return llist