Tuesday, 29 August 2017

Remove duplicate element from sorted Linked List

 
For example if the linked list is 11->11->11->21->43->43->60 then removeDuplicates() should convert the list to 11->21->43->60.

Solution:
  Node removeDuplicates(Node head)
    {
        Node current=head;
        Node nextNode;
        while(current.next != null)
        {
            if(current.data==current.next.data)
            {
                nextNode=current.next.next;
                current.next=null;
                current.next=nextNode;
            }
            else
            current=current.next;
        }
        return head;
    }

No comments:

Post a Comment