Skip to content

Instantly share code, notes, and snippets.

@bangarharshit
Created April 12, 2020 09:31
Show Gist options
  • Save bangarharshit/5959a0475b75663da95257b896281c2d to your computer and use it in GitHub Desktop.
Save bangarharshit/5959a0475b75663da95257b896281c2d to your computer and use it in GitHub Desktop.
// 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