删除链表中重复的结点

删除链表中重复的结点

题目描述

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

非递归版, 新建一个头结点

class Solution {
public:
    ListNode* deleteDuplication(ListNode* pHead)
    {
        ListNode *myHead = new ListNode(-1);
        myHead->next = pHead;
        ListNode *current = pHead;
        ListNode *pPreNode = myHead;
        while ((nullptr != current && (nullptr != current->next))) {
            if (current->val == current->next->val) {
                int val = current->val;    // 建一个临时变量保存val值, 一开始没绕过这个弯
                while ((nullptr != current) && (val == current->val)) {
                    ListNode *temp = current;
                    current = current->next;
                    delete temp;
                }
                pPreNode->next = current;
            }
            else {
                pPreNode = current;
                current = current->next;
            }
        }
        ListNode *ret = myHead->next;
        delete myHead;
        return ret;
    }
};

递归版

class Solution {
public:
    ListNode* deleteDuplication(ListNode* pHead)
    {
        if (nullptr == pHead) {
            return pHead;
        }
        if (nullptr == pHead->next) {
            return pHead;
        }
        ListNode *current = nullptr;
        if (pHead->val == pHead->next->val) {
            current = pHead->next;
            // 遍历链表找到一个节点不等于头结点的
            while ((nullptr != current) && (current->val == pHead->val)) {
                // 内存泄漏问题要临时变量
                ListNode *temp = current;
                current = current->next;
                delete temp;
            }
            return deleteDuplication(current);
        }
        else {
            current = pHead->next;
            pHead->next = deleteDuplication(current);
            return pHead;
        }
    }
};

剑指offer版

class Solution {
public:
    ListNode* deleteDuplication(ListNode* pHead)
    {
        if (nullptr == pHead) 
            return nullptr;
        ListNode *pPreNode = nullptr;
        ListNode *pNode = pHead;
        while(nullptr != pNode) {
            ListNode *pNext = pNode->next;
            bool needDelete = false;
            if ((nullptr != pNext) && (pNode->val == pNext->val)) {
                needDelete = true;
            }
            if (!needDelete) {    // 不需要删除
                pPreNode = pNode;
                pNode = pNode->next;
            }
            else {            // 需要删除
                int val = pNode->val;
                ListNode *pToBeDel = pNode;
                while ((nullptr != pToBeDel) && (pToBeDel->val == val)) {
                    pNext = pToBeDel->next;
                    // 释放pToBeDel, 若不知道pToBeDel怎么创建的, 要注释掉
                    delete pToBeDel;
                    pToBeDel = nullptr;
                    pToBeDel = pNext;
                }
                if (pPreNode == nullptr) {		// 删除的是头结点
                    pHead = pNext;
                }
                else {							// 不是头结点时, 要把pPreNode的next域指向pNext
                    pPreNode->next = pNext;
                }
                pNode = pNext;
            }
        }
        return pHead;
    }
};
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
        val(x), next(NULL) {
    }
};
原文地址:https://www.cnblogs.com/hesper/p/10542135.html