• (剑指Offer)面试题57:删除链表中的重复结点


    题目:

    在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。

    例如,链表1->2->3->3->4->4->5 处理后为 1->2->5

    思路:

    由于头结点有可能被删除,因此需要创建新的头结点pNew;

    遍历链表,每遍历一个结点pCur,且pCur!=NULL,做以下判断:

    如果它的下个结点不为空,且当前结点和下个结点的值相等,那么说明这两个是重复的结点,需要继续往下遍历,一直找到不重复的结点为止;

    否则,pNew的下个结点指向当前结点pCur,即pNew->next=pCur,pNew=pNew->next;

    在线测试OJ:

    http://www.nowcoder.com/books/coding-interviews/fc533c45b73a41b0b44ccba763f866ef?rp=3

    AC代码:
    (没有delete操作,容易造成内存泄露)

    /*
    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;
            
            ListNode* pNew= new ListNode(0);
            ListNode* pTmp=pNew;
            ListNode* pCur=pHead;
            
            while(pCur){
                if(pCur->next!=NULL && pCur->val==pCur->next->val){
                    ListNode* pNext=pCur->next;
                    while(pNext->next!=NULL && pNext->val==pNext->next->val)
                        pNext=pNext->next;
                    pCur=pNext->next;
                }
                else{
                    pTmp->next=pCur;
                    pTmp=pTmp->next;
                    pCur=pCur->next;
                }
            }
            
            pTmp->next=NULL;
            
            return pNew->next;
        }
    };
    

    (添加delete操作,避免内存泄露)

    /*
    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;
            
            ListNode* pNew= new ListNode(0);
            ListNode* pTmp=pNew;
            ListNode* pCur=pHead;
            ListNode* pDel=NULL;
            
            while(pCur){
                if(pCur->next!=NULL && pCur->val==pCur->next->val){
                    pDel=pCur;
                    ListNode* pNext=pCur->next;
                    delete pDel;
                    while(pNext->next!=NULL && pNext->val==pNext->next->val){
                        pDel=pNext;
                        pNext=pNext->next;
                        delete pDel;
                    }
                    pDel=pNext;
                    pCur=pNext->next;
                    delete pDel;
                }
                else{
                    pTmp->next=pCur;
                    pTmp=pTmp->next;
                    pCur=pCur->next;
                }
            }
            
            pTmp->next=NULL;
            
            return pNew->next;
        }
    };
     
  • 相关阅读:
    Chapter 6 GUI and OOD
    Chapter 5 : Control Structures 2 : Repetition
    Chapter 4 : Control Structures 1 : Selection
    Chapter 3 Introduction to Objects and Input/Output
    为什么很多应用都安装在/usr/local目录下?
    Chapter 2 Basic Elements of JAVA
    Chapter 1 An Overview of Computers and Programming Languages
    ZooKeeper Getting Started Guide
    The Apache HBase™ Reference Guide
    垃圾回收算法记录
  • 原文地址:https://www.cnblogs.com/AndyJee/p/4705936.html
Copyright © 2020-2023  润新知