zoukankan      html  css  js  c++  java
  • Leetcode 82

    有个错误就是member access within null pointer of type 'struct ListNode'  

    其实就是判断了指针是否异常了,比如NULL->next之类。要记得用new给节点初始化,而指针不需要初始化

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* deleteDuplicates(ListNode* head) {
            if(head == NULL) return head;
            ListNode* bighead = new ListNode(0);
            ListNode* pre;
            ListNode* move;
            
            bighead->next = head;
            pre = bighead;
            move = head;
            
            while(move != NULL && move->next != NULL){
                if(move->next->val == move->val){
                    while((move->next != NULL) && (move->next->val == move->val)){
                        move = move->next;                    
                    }
                    pre->next = move->next;
                    move = move->next;
                }
                else{
                    pre = pre->next;
                    move = move->next;
                }
            }
            return bighead->next;
        }
    };
  • 相关阅读:
    CAN
    snip_opencv环境配置和测试程序
    snip_进制转换代码段
    代码高亮的调试过程
    test3
    test2
    dsBlog_杂类
    js秒数转换为时分秒
    Linux 添加硬盘挂着到指定目录
    上海居住证积分续办
  • 原文地址:https://www.cnblogs.com/cunyusup/p/9919726.html
Copyright © 2011-2022 走看看