Created
April 12, 2020 09:31
-
-
Save bangarharshit/5959a0475b75663da95257b896281c2d to your computer and use it in GitHub Desktop.
This file contains 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
// Given a sorted linked list, delete all duplicates such that each element appear only once. | |
// For example, | |
// Given 1->1->2, return 1->2. | |
// Given 1->1->2->3->3, return 1->2->3. | |
/** | |
* Definition for singly-linked list. | |
* class ListNode { | |
* public int val; | |
* public ListNode next; | |
* ListNode(int x) { val = x; next = null; } | |
* } | |
*/ | |
public class Solution { | |
public ListNode deleteDuplicates(ListNode A) { | |
ListNode head = A; | |
int value = 0; | |
while (A != null) { | |
value = A.val; | |
while (A.next != null && A.next.val == value) { | |
A.next = A.next.next; | |
} | |
A = A.next; | |
} | |
return head; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment