/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } }; */ class Solution { public: ListNode* deleteDuplication(ListNode* pHead) { if (pHead == NULL || pHead->next == NULL) return pHead; ListNode *cur = pHead, *pre = NULL; while(cur) { ListNode* p = cur->next; bool needDelete = false; if (p && p->val == cur->val) { needDelete = true; } if (!needDelete) { pre = cur; cur = cur->next; } else { ListNode* pDelNode = cur; int value = cur->val; while(pDelNode && pDelNode->val == value) { ListNode* next = pDelNode->next; delete pDelNode; pDelNode = NULL; pDelNode = next; } if (pre == NULL) { pHead = pDelNode; } else { pre->next = pDelNode; } cur = pDelNode; } } return pHead; } };