题目描述
给出一个升序排序的链表,删除链表中的所有重复出现的元素,只保留原链表中只出现一次的元素。
例如:
给出的链表为1→2→3→3→4→4→5, 返回1→2→5.
给出的链表为1→1→1→2→3, 返回2→3.
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
/**
*
* @param head ListNode类
* @return ListNode类
*/
ListNode* deleteDuplicates(ListNode* head) {
// write code here
if(head==NULL || head->next==NULL){
return head;
}
ListNode* pHead = new ListNode(0);
pHead->next = head;
ListNode* pre = pHead;
ListNode* cur = head;
int count=0;
while(cur && cur->next){
if(cur->val == cur->next->val){
cur->next = cur->next->next;
count++;
}else{
if(count>0){