开发者社区 问答 正文

去除有序列表中的重复元素

给定一个有序链表,去除其中重复的元素,让每个重复的元素只显示一次,我是用单指针做的,但是很奇怪当输入为{1,1,....}这种头部元素重复的情况下不能去重,其它情况没有问题,我知道是头部元素没处理好,但到底是哪一行代码逻辑不对呢?代码如下:

struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};
class Solution
{
public:
    ListNode *deleteDuplicates(ListNode *head)
    {
        ListNode **pcur=&head;
        if(head==NULL||head->next==NULL){
        return head;
        }
        while((*pcur)->next!=NULL)
        {
            if((*pcur)->val==(*pcur)->next->val)
            {
                *pcur=(*pcur)->next;

            }
            else
            {
                pcur=&((*pcur)->next);

            }
        }
        return head;
    }
};

展开
收起
a123456678 2016-06-06 19:42:31 2275 分享 版权
1 条回答
写回答
取消 提交回答
  • #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<string>
    #include<queue>
    #include<map>
    #include<cmath>
    #include<stack>
    #include<algorithm>
    #include<functional>
    #include<stdarg.h>
    using namespace std;
    #ifdef __int64
    typedef __int64 LL;
    #else
    typedef long long LL;
    #endif
    
    struct ListNode {
        int val;
        ListNode *next;
        ListNode(int x) : val(x), next(NULL) {}
        ListNode(int x, ListNode *next) : val(x), next(next) {}
        ListNode():next(NULL) {}
    } str[100];
    ListNode *deleteDuplicates(ListNode *head) {
        while(head && head->next) {
            if(head->val==head->next->val) {
                head->next = head->next->next;
            } else {
                head = head->next;
            }
        }
        return head;
    }
    
    ListNode *oldDeleteDuplicates(ListNode *head) {
        ListNode **pcur=&head; //取得 head 变量的
        if(head==NULL||head->next==NULL) {//特判是不是没有元素或者只有一个元素
            return head;
        }
        /*
        这个时候 head 是 one 的地址。
        pcur 是 head 的地址。
        *pcur 就代表 head 了,即 one
    
        (*pcur)->nex 指向 two,所以不结束循环,且比较相等了
        所以你给 *pcur 赋值,也就是给 head 赋值。
        此时 *pcur 就指向  two 了。
    
        */
        while((*pcur)->next!=NULL) {
            if((*pcur)->val==(*pcur)->next->val) {
                *pcur=(*pcur)->next;
                // (*pcur)->next =((*pcur)->next->next);
            } else {
                pcur=&((*pcur)->next);
    
            }
        }
        return head;
    }
    
    void testAsignPoint(ListNode *head) {
        printf("    asign begin=%0x\n",head);
        head = head->next;
        printf("    asign begin=%0x\n",head);
    }
    
    void myprintf(ListNode* head) {
        while(head != NULL) {
            printf("%d ", head->val);
            head=head->next;
        }
        printf("\n");
    }
    
    void testAsignPointAgain(unsigned int addr){
        printf("    asign begin=%0x\n",addr);
        addr = (unsigned int)((ListNode *)addr)->next;//28fef8
        printf("    asign begin=%0x\n",addr);
    }
    
    void test(ListNode* ptest) {
    
    
        printf("ptest begin=%0x\n",ptest);//28fef0
        testAsignPoint(ptest);
        printf("one ptest =%0x\n",ptest);//28fef0
        printf("same as before code");
        testAsignPointAgain((unsigned int)(ptest));
        printf("one ptest =%0x\n",ptest);//28fef0
    
        printf("ptest=%0x\n",ptest);
        myprintf(ptest);
        oldDeleteDuplicates(ptest);
        myprintf(ptest);
        deleteDuplicates(ptest);
        printf("ptest=%0x\n",ptest);
        myprintf(ptest);
    }
    
    void testSample(){
        ListNode three(1, NULL);
        ListNode two(0, &three);
        ListNode one(0, &two);
        test(&one);
    
    }
    
    int main() {
        int n = 10;
        for(int i=0; i<n; i++) {
            str[i].val = i/2;
            str[i].next = &str[i+1];
        }
        str[n].val = n/2;
        str[n].next = NULL;
        printf("deleteDuplicates begin\n");
        myprintf(str);
        deleteDuplicates(&str[0]);
        myprintf(str);
        printf("deleteDuplicates end\n");
        printf("\n");
        printf("test Asign Point begin\n");
        testSample();
        printf("test Asign Point begin\n");
    
        return 0;
    }
    2019-07-17 19:29:19
    赞同 展开评论
问答地址: