letCode第二题题目如下:

  给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。

如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。您可以假设除了数字 0 之外,这两个数都不会以 0 开头。

事实上,这一题考的就是节点和一点数学知识,分析题意:两个链表,存储两个逆序数,从头结点开始,个位对个位,十位对十位,长度不一定相等,使用链表逆序输出结果。

所以使用一个标志位作为进位运算,代码如下:

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode * result = new ListNode();
int carry = ;
ListNode* curr = result;
//有进位,任意链表均有值
while(l1!=NULL || l2 != NULL | carry!=){
       //这里分开判断
if (l1 != NULL){
carry += l1->val;
l1 = l1->next;
}
if(l2 != NULL){
carry += l2->val;
l2 = l2->next;
}
ListNode* node = new ListNode(carry%);
//向下取整
carry/=;
curr->next = node;
curr = node;
}
return result;
}
};

letCode-2的更多相关文章

  1. letcode刷题之两数相加

    letcode里面刷题,坑还是链表不熟,(1)头结点还是有必要设置,否则返回的时候找不到位置:(2)先设置next到新节点再next到下一个节点:都是基础知识 /* * * You are given ...

  2. [py]letcode第一题求和

    letcode第一题, tm的不好弄. 想了很久想到了一个粗蠢的解决办法. Given an array of integers, return indices of the two numbers ...

  3. (letcode)String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  4. [Letcode] 1. Two Sum

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  5. 迭代法与开根号求值(letcode 69)

    p { margin-bottom: 0.25cm; line-height: 120% } 一.理论证明 p { margin-bottom: 0.25cm; line-height: 120% } ...

  6. 【letcode】5-LongestPalindromicSubstring

    回文串 回文串(palindromic string)是指这个字符串无论从左读还是从右读,所读的顺序是一样的:简而言之,回文串是左右对称的.一般求解一个字符串的最长回文子串问题. problem:Lo ...

  7. letcode code]Maximum Subarray

    1 题目: Find the contiguous subarray within an array (containing at least one number) which has the la ...

  8. letCode(771 Jewels and Stones )

    问题描述: You're given strings J representing the types of stones that are jewels, and S representing th ...

  9. letcode刷题记录-day03-罗马转整数

    题目 罗马转整数 题目描述 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 ...

  10. letcode刷题记录-day02-回文数

    回文数 题目描述 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target  的那 两个 整数,并返回它们的数组下标. 你可以假设每种输入只会对应一个答 ...

随机推荐

  1. C#简单打印出LIST集合

    循环打印集合,打印数组,随手写写,新手可以看看, 结果是不是有一些多余的0,把  int [][] ints =new int[3][];  改为new int[2][]; 运行出错,哈哈.  int ...

  2. Linux基础命令---sysctl修改内核参数

    sysctl sysctl指令用来修改正在运行的内核参数,可以修改的参数都保存在/proc/sys/目录中,修改会立即生效.Linux中的sysctl支持需要Procfs.您可以使用sysctl来读取 ...

  3. 2017-2018-2 20155303『网络对抗技术』Exp2:后门原理与实践

    2017-2018-2 『网络对抗技术』Exp2:后门原理与实践 --------CONTENTS-------- 1. 后门原理与实践实验说明 2. 常用后门工具 NC或netcat Win获得Li ...

  4. canutils上板测试问题记录

    ltp-ddt运行can_loopback时出错 pan(1881): Must supply a file collection or a command 原因runtest/ddt/can_loo ...

  5. openvpn 初步使用

    服务端:Centos 7.2 openvpn 2.4.3 客户端:Windows 10 安装包 openvpn的官网在国内访问不了,服务端通过yum安装,客户端在第三方网站下载的 一般的国内源应该都包 ...

  6. 【Python61--异常处理】

    一.URLrror 当我们的urlopen无法处理一个响应的时候就会出现一个urlerror的错误,但同时url会伴随一个res的属性,包含一个由错误编码和错误信息url 举例: import url ...

  7. Bootstrap 可视化HTML编辑器,summernote

    Bootstrap 可视化HTML编辑器之summernote,用其官网上的介绍就是"Super Simple WYSIWYG editor",不过在我看来,与bootstrap中 ...

  8. oracle常用的数据字典查询语句

    select * from all_source where owner='user_name' and type = 'PROCEDURE' and upper(text) like upper(' ...

  9. Learning-Python【16】:模块的导入使用

    一.什么是模块 模块就是一系列功能的集合体,一个模块就是一个包含了Python定义和声明的文件,文件名就是模块名字加上.py的后缀. 模块有三种来源: 1.内置的模块 2.第三方的模块 3.自定义模块 ...

  10. C#时间戳的简单实现

    Introduction: 在项目开发中,我们都经常会用到时间戳来进行时间的存储和传递,最常用的Unix时间戳(TimeStamp)是指格林尼治时间1970年1月1日0时(北京时间1970年1月1日8 ...