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. POJ 2154 color (polya + 欧拉优化)

    Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). You ...

  2. python IO 多路复用

    一.epoll epoll 参考链接: https://www.cnblogs.com/Alanpy/articles/5125986.html epoll  参考链接: https://www.cn ...

  3. url的参数解析成key-value

    function urlController(url) { var _url = url.split("?")[1]; if(!_url){ return {}; } var wi ...

  4. 教你如何在win7中的cygwin64下安装hadoop

    首先我们要准备如下环境及软件: win7(64位) cygwin - jdk-6u25-windows-x64.zip hadoop-.tar.gz 1.在win7系统上正常安装jdk,同时注意设置好 ...

  5. JAX-WS 使用maven创建

    maven 创建jar jar包依赖 <dependency> <groupId>junit</groupId> <artifactId>junit&l ...

  6. Python文件学习

    Python文件学习 文章 Python文件学习 open函数 基本的用法模式:file_object=open('',access_mode='r',buffering=-1) 其中access_m ...

  7. Django-debug-toolbar的使用

    Django项目的开发过程中,离不开各种调试,那么,我们今天介绍一个工具,django debug toolbar,该工具为我们提供了更加丰富的调试信息,如提供session信息,SQL查询信息等等. ...

  8. newcoder 小A的柱状图(单调栈)题解

    题目描述 柱状图是有一些宽度相等的矩形下端对齐以后横向排列的图形,但是小A的柱状图却不是一个规范的柱状图,它的每个矩形下端的宽度可以是不相同的一些整数,分别为a[i] 每个矩形的高度是h[i] ,现在 ...

  9. hdu 5564 Clarke and digits 矩阵快速幂优化数位dp

    Clarke and digits Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  10. Linux下修改用户的UID、GID

    01.用户的UID和GID不能被占用 [root@26 ~]# id mvpuid=503(mvp) gid=503(mvp) groups=503(mvp) ###假定我需要设置mvp的uid/gi ...