只适合有序,无序链表需要用哈希表

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
        val(x), next(NULL) {
    }
};
*/
class Solution {
public:
    ListNode* deleteDuplication(ListNode* pHead) {
      if (pHead == nullptr || pHead->next == nullptr) {
        return pHead;
      }
      
      auto head = std::make_unique<ListNode>(-1);
      head->next = pHead;
      ListNode *pre = head.get();
      ListNode *cur = pHead->next;
      
      while (cur) {
        if (cur->val != pre->next->val) {
          cur = cur->next;
          pre = pre->next;
        } else {
          ListNode *tmp = pre->next;
          //  跳过重复结点
          while (tmp && tmp->val == cur->val) {
            tmp = tmp->next;
          }
          
          cur = tmp;
          tmp = pre->next;
          pre->next = cur;
          
          ListNode *nex = tmp->next;
          
          //  删除结点
          while (nex != cur) {
            delete tmp;
            tmp = nex;
            nex = nex->next;
          }
          
          delete tmp;
          
          if (cur) {
            cur = cur->next;
          }
        }
      }
      
      return head->next;
    }
};