剑指offer 66题 -- 删除链表中重复的节点

/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* deleteDuplication(ListNode* pHead)
{

  //变量定义区
  ListNode* pNode = pHead;
  int val =0;
  ListNode* pResult = new ListNode(val);
  if(pResult == NULL)
    return NULL;
  pResult->next = pHead;
  ListNode* pPrevious = pResult;
  pPrevious->next = pHead;

  //入参有效性检查
  if(pHead == NULL )
    return pHead;

  while(pNode != NULL && pNode->next != NULL)
  {
    if(pNode->next->val == pNode->val)
    {
      val = pNode->val;
      while(pNode != NULL && (pNode->val == val) )
      {
        ListNode* tmp = pNode;
        pNode = pNode->next;
        delete tmp;
      }
    pPrevious->next = pNode;
    }
    else
    {
      pPrevious = pPrevious->next;
      pNode = pNode->next;

    }
  }

  return pResult->next;

}
};

posted @ 2017-02-26 23:14  夜雨寒山  阅读(88)  评论(0编辑  收藏  举报