leetcode144add-two-numbers
题目描述
The digits are stored in reverse order and each of their nodes contain a
single digit. Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
/**
*
* @param l1 ListNode类
* @param l2 ListNode类
* @return ListNode类
*/
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
// write code here
//创建结果链表的头结点,默认该节点中的value为-1
ListNode dummy=new ListNode(-1);
ListNode pre=dummy;
//进位标识carry,默认为0
int carry =0;
//遍历链表,当两个链表都为空时,退出
while (l1!=null || l2 !=null){
//判断该节点是否为空,当节点为空时,用0补全,不为空时,加数即为节点的值
int d1=(l1==null) ?0:l1.val;
int d2=(l2==null)? 0:l2.val;
//对节点求和,注意:求和是需要考虑到进位
int sum=d1+d2+carry;
//更新进位标识,
carry=(sum>=10)?1:0;
//sum%10标识求和的个位数,将其保存到结果链表中
pre.next=new ListNode(sum%10);
pre=pre.next;
if (l1!=null) l1=l1.next;
if (l2!=null) l2=l2.next;
}
//重点 这是一个特殊情况,当两个链表计算完后
//还需要判断进位标识是否为1,如果为1,如28+81=104,需要创建一个节点保存最高位
if (carry ==1)
pre.next=new ListNode(1);
return dummy.next;
}
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
/**
*
* @param l1 ListNode类
* @param l2 ListNode类
* @return ListNode类
*/
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
// write code here
ListNode fake(0);
ListNode *p=&fake;
int carry=0;
while (l1 || l2 || carry)
{
int sum=(l1?l1->val:0)+(l2?l2->val:0)+carry;
carry=sum/10;
p->next=new ListNode(sum%10);
p=p->next;
l1=l1?l1->next:nullptr;
l2=l2?l2->next:nullptr;
}
return fake.next;
}
};
leetcode144add-two-numbers的更多相关文章
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Bitwise AND of Numbers Range 数字范围位相与
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- [LeetCode] Valid Phone Numbers 验证电话号码
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- [LeetCode] Consecutive Numbers 连续的数字
Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...
- [LeetCode] Compare Version Numbers 版本比较
Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...
- [LeetCode] Sum Root to Leaf Numbers 求根到叶节点数字之和
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
随机推荐
- centos7下安装fabric2.2
准备基础环境 1.安装curl.git yum install curl yum install git 2.go环境搭建 下载解压 cd /home mkdir app cd app wget ht ...
- try-finally的时候try里面带return
最近学习的JVM小册中老师提了个问题: 最开始我觉得是1,结果程序跑出来是0,感到很疑惑,于是查看了下字节码: 从字节码可以看出: 0:定义变量0 1:将0存入本地变量表slot-0 2:加载slot ...
- 基于raft共识搭建的Fabric1.4.4多机网络环境
1准备工作介绍 1各个主机ip以及节点分配情况 各个主机的节点分配情况 ip地址 orderer0.example.com,peer0.org1.example.com 172.17.3.60 ord ...
- docker-管理容器常用命令
1. docker-管理容器常用命令 2. docker管理容器常用命令 1) docker ps 显示当前正在运行的容器 [root@test-1 ~]# docker ps CONTAINER ...
- 多测师讲解python_模块间的调用_高级讲师肖sir
案例1: 在aaa.py 文件A类中定义一个函数sadp: 在bbb.py文件中导入aaa模块,导入类 ,调用函数 案例2: aaa模块中定义一个A类, 在定义一个sadp的函数, 在bbb模块中导 ...
- requirements基本使用
requirements作用描述:很多 Python 项目中经常会包含一个 requirements.txt 文件,里面内容是项目的依赖包及其对应版本号的信息列表,即项目依赖关系清单,其作用是用来重新 ...
- centos6.8 配置 yum 仓库
挂载方式 mkdir /mnt/cdrom 加载光盘: mount /dev/cdrom /mnt/cdrom/ 挂载光盘到 /mnt/cdrom/ 挂载成功 cd /etc/yum.repos.d/ ...
- C++ Primer第5版 第二章课后练习
练习2.1 C++ 语言规定short 和 int 至少 16 位, long 至少32位, long long 至少64位.带符号类型可以表示整数.负数或0, 无符号类型则仅能表示大于等于0的值Th ...
- linux 安装配置zerotier
1.在线安装zerotier curl -s https://install.zerotier.com/ | sudo bash 2.添加开机自启 $ sudo systemctl enable ze ...
- Django (学习第二部 ORM 模型层)
Django对数据库的操作 Django的 ORM 简介 ORM操作 (增删改查) ORM操作数据库的增删改查 ORM创建表关系 ORM中常用字段及参数 数据库的查询优化 ORM中如何开启事务 ORM ...