Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.