Skip to content

Instantly share code, notes, and snippets.

@si-yao
Created September 12, 2019 06:22
Show Gist options
  • Save si-yao/55bd5a1cf4c6dbaf3030a0f7670bdf0e to your computer and use it in GitHub Desktop.
Save si-yao/55bd5a1cf4c6dbaf3030a0f7670bdf0e to your computer and use it in GitHub Desktop.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode ptr = dummy;
while (ptr.next != null) {
if (ptr.next.next != null && ptr.next.val == ptr.next.next.val) {
int del = ptr.next.val;
while (ptr.next != null && ptr.next.val == del) {
deleteNext(ptr);
}
} else {
ptr = ptr.next;
}
}
return dummy.next;
}
private void deleteNext(ListNode node) {
node.next = node.next.next;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment