Created
January 4, 2021 10:55
-
-
Save Ram-1234/bdb36fda9b11d6bd4b7df01ab57d650e to your computer and use it in GitHub Desktop.
Delete duplicate-value nodes from a sorted linked list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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