Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Ram-1234/bdb36fda9b11d6bd4b7df01ab57d650e to your computer and use it in GitHub Desktop.
Save Ram-1234/bdb36fda9b11d6bd4b7df01ab57d650e to your computer and use it in GitHub Desktop.
Delete duplicate-value nodes from a sorted linked list
Expalnation
input:- 1->2->2->3->3->3->4->null
output:-1->2->3->4->null
############################JAVA CODE######################################
// Complete the removeDuplicates function below.
/*
* For your reference:
*
* SinglyLinkedListNode {
* int data;
* SinglyLinkedListNode next;
* }
*
*/
static SinglyLinkedListNode removeDuplicates(SinglyLinkedListNode head) {
SinglyLinkedListNode curr=head;
//SinglyLinkedListNode prev=null;
while(curr!=null){
SinglyLinkedListNode prev=curr;
while(prev!=null && prev.data==curr.data){
prev=prev.next;
}
curr.next=prev;
curr=curr.next;
}
return head;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment