zoukankan      html  css  js  c++  java
  • LeetCode runtime error

    今天在写LeetCode的某一道题目时候,遇到runtime error问题,本地能过,submit后死活不能通过。

    查了一下网上的一些答案,基本上都是数组、指针没有初始化造成野指针、数组索引值越界。

    看了自己的代码觉得没啥问题,没有到数组,那只能够是链表的指针变成野指针的问题。仔细看了两遍代码,终于发现是链表里的random指针没有初始化,在调用的时候很容易野。

    Problem:

    给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。

    要求返回这个链表的深度拷贝。

    有问题的代码片段:

    while(currentNode != NULL)
        {
            temp = (struct RandomListNode*)malloc(sizeof(struct RandomListNode));
            temp->label = currentNode->label;
            temp->next = currentNode->next;
            currentNode->next = temp;
            
            currentNode = temp->next;
        }
        

    加了初始化指针的代码:

    while(currentNode != NULL)
        {
            temp = (struct RandomListNode*)malloc(sizeof(struct RandomListNode));
            temp->label = currentNode->label;
            temp->next = currentNode->next;
            temp->random = NULL; //就是这里出的问题,如果没有初始化成NULL的话,指向的为止不知道是哪里,服务器会访问出错
            currentNode->next = temp;
            
            currentNode = temp->next;
        }
  • 相关阅读:
    [MySQL]You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
    mysql的索引
    Mysql中的Btree与Hash索引
    Tomcat集群的session共享
    Linux常用命令总结
    docker elk
    docker+mysql+zabix-server环境搭建
    centos7系统服务管理
    Linux vim常用命令
    linux系统日志查看
  • 原文地址:https://www.cnblogs.com/sonwendi/p/10011791.html
Copyright © 2011-2022 走看看