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 ...
随机推荐
- 一个Java对象的内存布局
1.对象的创建过程 class loading class linking(verification,preparation,resolution) class initializing 申请对象内存 ...
- AngularJS应用访问Android手机画廊
下载angularjs.zip - 4.5 KB 介绍 本文演示了如何使用AngularJS调用Android应用程序公开的REST api来查看图库. 背景 Android和iOS都有相当多的远程访 ...
- shell-脚本开发基本规范及习惯
1.shell-脚本开发基本规范及习惯 1.开头指定脚本解析器 #!/bin/sh 或#!/bin/bash 2.开头加版本版权等信息 #Date: 2018/3/26 #Author: zhangs ...
- 手写一个HTTP框架:两个类实现基本的IoC功能
jsoncat: 仿 Spring Boot 但不同于 Spring Boot 的一个轻量级的 HTTP 框架 国庆节的时候,我就已经把 jsoncat 的 IoC 功能给写了,具体可以看这篇文章&l ...
- redis协议规范
好多年前看过redis的代码,那个时候还是2.6的版本,集群和哨兵还没加入正式代码,这几年redis发展的好快.简略翻译一篇文章redis的https://redis.io/topics/protoc ...
- bootStrap小结2
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Conten ...
- 界面酷炫,功能强大!这款 Linux 性能实时监控工具超好用!
对于维护.管理Linux系统来说,它的性能监控非常重要,特别是实时监控数据,这个数据有利于我们判断服务器的负载压力,及时调整资源调配,也有助于更好的服务于业务.所以,今天民工哥给大家安利一款 Linu ...
- navicate premium黄色版本破解下载
百度网盘下载 提取码: tsua 按照电脑安装32位或者64位 安装完成后点击最后一个进行破解汉化
- js 判断客户端 和 asp.net/C#判断客户端类型
1.js 判断客户端 <script language="JavaScript"> <!-- onload = function browserRedirect( ...
- C# Webservice中如何实现方法重载--(方法名同名时出现的问题)
本文摘抄自:http://blog.sina.com.cn/s/blog_53b720bb0100voh3.html 1.Webservice中的方法重载问题(1)在要重载的WebMethod上打个M ...