Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Desolve/6e999e9f0ca340173be77592141b0a94 to your computer and use it in GitHub Desktop.
Save Desolve/6e999e9f0ca340173be77592141b0a94 to your computer and use it in GitHub Desktop.
0082 Remove Duplicates from Sorted List II
/**
* 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 head;
ListNode dum = new ListNode(0);
dum.next = head;
ListNode pre = dum;
ListNode cur = head;
while (cur != null) {
while (cur.next != null && cur.val == cur.next.val) {
cur = cur.next;
}
if (pre.next == cur) {
pre = pre.next;
} else {
pre.next = cur.next;
}
cur = cur.next;
}
return dum.next;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment