【剑指offer】删除链表中重复的结点

题目描述

在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表 1 − > 2 − > 3 − > 3 − > 4 − > 4 − > 5 1->2->3->3->4->4->5 1>2>3>3>4>4>5 处理后为 1 − > 2 − > 5 1->2->5 1>2>5

思路:

1)非递归方式。先建立一个新节点 n e w H e a d newHead newHead,使其 n e x t next next指向 p H e a d pHead pHead,再令一个 p r e pre pre指向 n e w H e a d newHead newHead;遍历原链表,比较当前节点 p p p的值与下一节点 n e x t next next值的大小,如果不相等,则比较下一个节点 p − > n e x t p->next p>next与其下一个节点 n e x t − > n e x t next->next next>next;如果相等,则从 n e x t next next节点开始,遍历比较后面的节点是否等于 n e x t next next节点的值,直到不相等为止,将 n e x t next next节点置于 p r e pre pre节点之后,相当于删除重复节点。然后 p = n e x t p = next p=next,从 n e x t next next开始下一次判断,直到为空

/*
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;
          else
          {
              //新建一个节点,防止头结点要被删除
              ListNode* newHead=new ListNode(-1);
              newHead->next=pHead;
              ListNode* pre=newHead;
              ListNode* p=pHead;
              ListNode* next=NULL;
              while(p!=NULL && p->next!=NULL)
              {
                  next=p->next;
                  if(p->val==next->val)//如果当前节点的值和下一个节点的值相等
                  {
                      while(next!=NULL && next->val==p->val)//向后重复查找
                          next=next->next;
                    pre->next=next;//指针赋值,就相当于删除
                    p=next;
                  }
                  else//如果当前节点和下一个节点值不等,则向后移动一位
                  {
                      pre=p;
                      p=p->next;
                  }
              }
           return newHead->next;//返回头结点的下一个节点
                
          }
    }
};

2)递归方式。判断当前节点的值与下一节点的值是否相等,如果不相等,令 p N o d e = p H e a d − > n e x t pNode = pHead->next pNode=pHead>next,为了寻找被删除之后的链表的下一节点,将 p N o d e pNode pNode作为参数传给函数,返回的即是 p H e a d − > n e x t pHead->next pHead>next;如果相等,那么令 p N o d e = p H e a d − > n e x t − > n e x t pNode = pHead->next->next pNode=pHead>next>next,从 p N o d e pNode pNode开始,判断接下来节点的值是否与 p H e a d pHead pHead的值相等,若相等,则继续遍历,否则将 p N o d e pNode pNode作为参数传给函数,返回该函数即可得到被删除重复节点后的链表

/*
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;
        ListNode* pNode;
        if (pHead->next->val == pHead->val) {
            pNode = pHead->next->next;
            while (pNode != nullptr && pNode->val == pHead->val)
                pNode = pNode->next;
            return deleteDuplication(pNode);
        } else {
            pNode = pHead->next;
            pHead->next = deleteDuplication(pNode);
            return pHead;
        }
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值