zoukankan      html  css  js  c++  java
  • 剑指offer55-删除链表中重复节点

    题目描述

    在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
        ListNode* deleteDuplication(ListNode* pHead)
        {
            //这里的重复节点与例题中类似,连续出现
            //if(pHead==NULL) return pHead;
            //考虑全链表重复情况
            ListNode*head=new ListNode(0);
            head->next=pHead;
            ListNode*p=head,*pre=head;
            while(p&&p->next)
            {
                
                if(p->val==p->next->val)
                {
                    while(p&&p->next&&p->val==p->next->val)
                    p=p->next;  
                 
                          pre->next=p->next;
                        //非删除情况
                      
                }else
                {
                   pre=p;
                }
                    p=p->next;
            }
            return head->next;
        }
  • 相关阅读:
    printf,wprintf与setlocale,char与wchar_t区别
    C++常量表达式、const、constexpr(C++11新增)的区别
    珍珠项链 Beads
    A Horrible Poem
    三个朋友
    Seek the Name, Seek the Fame
    Power Strings
    图书管理
    子串查找
    山峰和山谷 Ridges and Valleys
  • 原文地址:https://www.cnblogs.com/trouble-easy/p/12988138.html
Copyright © 2011-2022 走看看