Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2
, return 1->2
.
Given 1->1->2->3->3
, return 1->2->3
.
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
if(head == NULL || head->next == NULL)
return head;
ListNode *pre = head;
ListNode *p_next = pre;
while(pre != NULL)
{
p_next = pre->next;
if(p_next && p_next->val == pre->val)
{
ListNode *p_del = p_next;
pre->next = p_next->next;
delete p_del;
}
else
{
pre = pre->next;
}
}
return head;
}
};