在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表 1->2->3->3->4->4->5 处理后为 1->2->5
数据范围:链表长度满足 0≤n≤1000 ,链表中的值满足 1≤val≤1000
进阶:空间复杂度 O(n) ,时间复杂度 O(n)
例如输入{1,2,3,3,4,4,5}时,对应的输出为{1,2,5},对应的输入输出链表如下图所示:
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* deleteDuplication(ListNode* pHead) {
if(not pHead) return pHead;
ListNode* p = new ListNode(-1);
p->next = pHead;
ListNode* cur = p;
while(cur->next and cur->next->next){
if(cur->next->val==cur->next->next->val){
int temp = cur->next->val;
while(cur->next and cur->next->val==temp)
cur->next=cur->next->next;
}else
cur = cur->next;
}
return p->next;
}
};