Skip to content

Instantly share code, notes, and snippets.

@si-yao
Created September 12, 2019 06:36
Show Gist options
  • Save si-yao/678096464c8ac5ccddb006f5a696e0d2 to your computer and use it in GitHub Desktop.
Save si-yao/678096464c8ac5ccddb006f5a696e0d2 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) {
if (head == null) {
return null;
}
if (head.next == null || head.val != head.next.val) {
head.next = deleteDuplicates(head.next);
return head;
} else {
ListNode ptr = head;
while (ptr != null && ptr.val == head.val) {
ptr = ptr.next;
}
return deleteDuplicates(ptr);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment