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. windows 允许其他电脑访问本地mysql数据库

    第一步:用ping命令测试两台电脑是否连通 如果两台电脑是连通的请转到第二步,如果是非连通的请进行如下操作: 1.进入控制面板,打开Windows Defender 防火墙,点击高级设置(本人用的是W ...

  2. 分配swap分区

    1.free命令 用来查看swap分区的使用情况[root@localhost ~]#free#查看内存与swap分区使用状况◆cached(缓存):是指把读取出来的数据保存在内存当中,当再次 读取时 ...

  3. DataTable插件报错:Uncaught TypeError: Cannot read property 'style' of undefined

    DataTable插件报错:Uncaught TypeError: Cannot read property 'style' of undefined 原因:table 中定义的列和aoColumns ...

  4. Redis学习-sorted set数据类型

    sorted set 是有序集合,它在 set 的基础上增加了一个顺序属性,这一属性在添加修 改元素的时候可以指定,每次指定后,会自动重新按新的值调整顺序. zadd key score member ...

  5. 安卓内嵌H5只展示部分静态页面

    问题: 安卓内嵌H5在华为P9部分机型只展示h5静态页面无法展示接口返回渲染的页面 解决办法: Android  关闭硬件加速   android:hardwareAccelerated=" ...

  6. EJB到底是什么?

    EJB到底是什么?   1. 我们不禁要问,什么是"服务集群"?什么是"企业级开发"? 既然说了EJB 是为了"服务集群"和"企业 ...

  7. IIS 设备未就绪。

    看看Web.config 是否指向的磁盘在本机上不存在此磁盘

  8. C++单元测试gtest【搬砖】

    https://www.cnblogs.com/fnlingnzb-learner/p/6927834.html

  9. 没有显示器、网线、路由器,编辑TF卡连接树莓派

    只有电脑,连接树莓派的方法 电脑新建热点 打开TF卡,在根目录新建文件wpa_supplicant.conf,内容如下 country=GB ctrl_interface=DIR=/var/run/w ...

  10. Rancher2.0中部署Longhorn分布式存储实验

    目录 1.简介 2.实验环境 3.应用商店中部署longhorn 4.创建工作负载,使用longhorn存储 5.查看longhorn UI 6.注意事项 1.简介: Longhorn是Rancher ...