码迷,mamicode.com
首页 > 其他好文 > 详细

剑指offer57 删除链表中重复的结点

时间:2017-07-17 17:17:11      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:==   turn   lis   val   ted   调用   不同   span   off   

错误代码:

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

正确代码:

class Solution {
public:
    ListNode* deleteDuplication(ListNode* pHead)
    {
        if(pHead == NULL)
            return NULL;
        if(pHead->next==NULL)
            return pHead;
        ListNode* current = NULL;
        if(pHead->val == pHead->next->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;
        }
        return pHead;
    }
};

可以看到不同点只是把current->val == pHead->val和current != NULL换了一下位置

错误的代码报错报的是:段错误:您的程序发生段错误,可能是数组越界,堆栈溢出(比如,递归调用层数太多)等情况引起

剑指offer57 删除链表中重复的结点

标签:==   turn   lis   val   ted   调用   不同   span   off   

原文地址:http://www.cnblogs.com/ymjyqsx/p/7196125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!