今天在写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;
}

LeetCode runtime error的更多相关文章

  1. com.sun.xml.internal.ws.server.ServerRtException: Server Runtime Error: java.net.BindException: Cannot assign requested address: bind

    在发布 web service 时报错: Endpoint.publish(publishAddress, hl7MessageReveiver); com.sun.xml.internal.ws.s ...

  2. Arcmap 安装完后使用出现visual fortran run-time error的解决方法

    今天将ArcGIS安装到自己的XP笔记本上,安装过程一帆风顺,但打开Arcmap使用的时候,出现了visual fortran run-time error. 下面是解决方法: 下载个Dforrt.d ...

  3. Qt Sqlite qwt 发布过程中碰到的问题runtime error

    qt版本:4.8.0 qwt版本:6.1.2 使用dll show检测缺少的dll,或者笨一点的方法,点击运行差什么找什么放进去: 左上显示exe调用哪些dll,右边是dll又再次调用啦哪些dll: ...

  4. Codeforce - Runtime Error

    Bahosain was trying to solve this simple problem, but he got a Runtime Error on one of the test case ...

  5. IE Unknown runtime error

    1. 在函数中使用原生的js的时候,有时在IE下会出现Unknown runtime error 火狐下正常 2. 解决办法, 将原生js改成jquery处理兼容问题 document.getElem ...

  6. Microsoft Visual C++ Runtime error解决方法

    1: 当出现下图时提示Microsoft Visual C++ Runtime error 2:此时不要关闭该对话框,然后打开任务管理器(Ctrl+Shift+Esc)如下图: 找到Microsoft ...

  7. Microsoft Visual C++ Runtime Library Runtime Error的解决的方法

    打开浏览器时,出现Microsoft Visual C++ Runtime Library Runtime Error错误,初步预计是软件冲突,可能有多种出错的方式,我的是浏览器自己主动关闭. 一. ...

  8. UVa 1402 Runtime Error 伸展树

    Runtime Error 到现在连样例也跑不出来!!! 调试了一晚上快要死了…… 知道错在哪里但是不会改,代码先扔在这里吧.看来不能太依赖模板啊orz…… #include <cstdio&g ...

  9. Mindjet MindManager 2012 从模板创建出现“Runtime Error pure virtual function call” 解决方法

    我的Mindjet MindManager 2012 Pro也就是MindManager10 在应用模板之后总会显示 Microsoft Visual C++ Runtime Library Runt ...

随机推荐

  1. JS回调函数怎么写的?

    相信每个做前端的同学都用过太多太多的回调函数, 接下来就看看回调函数是怎么来的. 这玩意儿也没那么神秘,直接看代码: 声明函数的时候,把回调函数用作参数,并且在函数内调用它 function getD ...

  2. KVM原理及使用

    Qemu 和 Qemu-kvm Qemu: http://qemu-project.org/Download Qemu-kvm:https://sourceforge.net/projects/kvm ...

  3. android常见错误之 No resource found that matches the given name

    新手上路,还希望大神多多照顾,刚自学android,遇到很多困难.其中就有这个问题,不知道你们遇到过没有,反正我是很头痛. No resource found that matches the giv ...

  4. 【技术博客】MySQL和Django常用操作

    MySQL和Django是搭建网站常用的配置之一,在此记录一下在Windows系统搭建网站时MySQL以及Django常用的操作. MySQL MySQL的SQL语句不区分大小写,推荐将保留字大写,数 ...

  5. 最长公共子序列长度(dp)

    /// 求两个字符串的最大公共子序列长度,最长公共子序列则并不要求连续,但要求前后顺序(dp) #include <bits/stdc++.h> using namespace std; ...

  6. NIO网络编程

    1.创建服务端代码 public class NioServer { private static Map<String, SocketChannel> clientMap = new H ...

  7. NOTIC: [8] Trying to get property of non-object

      NOTIC: [8] Trying to get property of non-object /home/wwwroot/qwsd/Application/Admin/Controller/Pr ...

  8. UPDATE REPLACE 替换"\\"

    UPDATE Working_InterfaceToManager SET StudyPhotoInfoList = REPLACE(StudyPhotoInfoList,"\\\\&quo ...

  9. vue-cli webpack打包开启Gzip 报错—— Cannot find module 'compression-webpack-plugin

    异常描述: 复用以前框架,打包的时候报异常提示: Cannot find module 'compression-webpack-plugin" 然后安装插件: npm install -- ...

  10. 什么是依赖注入 IoC

    设计原则:依赖注入原则 依赖倒置原则,是一种程序设计模式的原则 高层模块不应该依赖底层模块,二者都应该依赖其抽象. 抽象不应该依赖细节,细节应该依赖抽象.依赖导致原则很好的体现了“面向接口编程”的思想 ...