1. Description
Given a sorted linked list, delete all duplicates such that each element appear only once.
2. Example
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
3. Code
public class LeetCode0083 {
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
public ListNode deleteDuplicates(ListNode head) {
ListNode dummy = new ListNode(Integer.MIN_VALUE);
dummy.next = head;
ListNode p = head;
ListNode pre = dummy;
while (p != null) {
if (p.val == pre.val) {
p = p.next;
} else {
pre.next = p;
pre = p;
p = p.next;
}
}
pre.next = null;
return dummy.next;
}
}
Comments | NOTHING