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
.
// Source : https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/
// Author : Chao Zeng
// Date : 2014-12-20
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
//特别要注意数据有多个重复的情况
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head){
if (!head || head->next == NULL)
return head;
ListNode *curr = head;
ListNode *pnext = head->next;
while (pnext != NULL){
if (pnext->val == curr->val){
curr->next = pnext->next;
}
else{
curr = pnext;
}
pnext = pnext->next;
}
return head;
}
};