删除链表中重复的结点

删除链表中重复的结点

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

class Solution {
public:
    ListNode* deleteDuplication(ListNode* pHead)
    {
    if (pHead==NULL)
        return NULL;
    if (pHead!=NULL && pHead->next==NULL)
        return pHead;
    ListNode* current;
    if (pHead->next->val==pHead->val){
        current=pHead->next->next;
        while(current != NULL && current->val == pHead->val)
            current=current->next;
        return deleteDuplication(current);
    }
    else {
        current=pHead->next;
        pHead->next = deleteDuplication(current);
        return pHead;
     }

    }
};
*/

class Solution {
public:
    ListNode* deleteDuplication(ListNode* pHead)
    {
        ListNode *tmp;
        bool flag = false;
        ListNode **p = &pHead;
        if(!pHead)
            return NULL;
        while(*p){
            int val = (*p)->val;
            flag = false;
            tmp = (*p)->next;
            while(tmp && tmp->val == val){
                flag = true;
                tmp = tmp->next;
            }
            //发生了重复 修改当前指向的结点为后面第一个不是val的结点
            if(flag){
                *p = tmp;
            }
            //没发生重复 修改p指向原本结点的next结点
            else{
                p = &((*p)->next);
            }
        }
        return pHead;
    }
};
    
    
    
class Solution {
public:
    ListNode* deleteDuplication(ListNode* pHead){
        ListNode* head = new ListNode(0);
        ListNode* p = head;
        ListNode* q = pHead;
        while(q) {
            while(q!=NULL&&q->next!=NULL&&q->next->val==q->val){
                int tmp=q->val;
                while(q!=NULL && q->val==tmp)
                    q=q->next;
            }
            p->next=q;
            p=p->next;
            if (q)
                q=q->next;
                
        }
        return head->next;
        
    }
};
    
    
*/python 解法
class Solution:
    def deleteDuplication(self, pHead):
        # write code here
        if not pHead or not pHead.next:
            return pHead
        if pHead.val == pHead.next.val:
            temp = pHead.next
            while temp and temp.val == pHead.val:
                temp = temp.next
            return self.deleteDuplication(temp)
        else:
            pHead.next = self.deleteDuplication(pHead.next)
            return pHead
            
            
            
*/
class Solution:
    def deleteDuplication(self,pHead):
        res = []
        while pHead:
            res.append(pHead.val)
            pHead=pHead.next
        res = list(filter(lambda c:res.count(c) == 1,res))
        dummy = ListNode(0)
        pre = dummy
        for i in res:
            node = ListNode(i)
            pre.next = node
            pre = pre.next
        return dummy.next
        
        
*/
class Solution:
    def deleteDuplication(self, pHead):
        # write code here
        if not pHead:
            return pHead
        #为了防止头结点是重复的,定义一个新结点指向头结点
        temp = ListNode(0)
        temp.next = pHead
        pre, nex = temp, pHead
        while nex:
            if nex.next and nex.next.val == nex.val:
                #如果出现了和当前节点相同的值的时候,当前节点前移一位
                while nex.next and nex.next.val == nex.val:
                    nex = nex.next
                pre.next = nex.next#只是指向,并没有动pre,相当于删除
                nex = nex.next
            else:
                pre = pre.next
                nex = nex.next
        return temp.next
                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值