链表 -- 2.2.1 Add Two Numbers -- 图解
描述
You are given two linked lists representing two non-negative numbers. e 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
class Solution{
public:ListNode *addTwoNumbers(ListNode *l1,ListNode *l2){ListNode head(-1);int carry = 0;ListNode *prev = &head;for(ListNode *pa = l1, *pb = l2;pa != nullptr || pb != nullptr;pa = pa == nullptr ? nullptr : pa->next,pb = pb == nullptr ? nullptr : pb->next,prev = prev->next){const int ai = pa == nullptr ? 0 : pa-val;const int bi = pb == nullptr ? 0 : pb->val;const int value = (ai + bi + carry) % 10;carry = (ai + bi + carry) / 10;prev->next = new ListNode(value);}if(carry > 0)prev->next = new ListNode(carry);return head.next;}
};
参考资料:
LeetCode题解
链表 -- 2.2.1 Add Two Numbers -- 图解相关推荐
- LeetCode 445. Add Two Numbers II--面试算法题--C++,Python解法
题目地址:Add Two Numbers II - LeetCode You are given two non-empty linked lists representing two non-neg ...
- [Leetcode] 445. Add Two Numbers II
问题: https://leetcode.com/problems/add-two-numbers-ii/#/description 思路:该题与"415. Add Strings" ...
- LeetCode之Add Two Numbers
LeetCode之Add Two Numbers 题目:You are given two linked lists representing two non-negative numbers. Th ...
- leetcode算法—两数相加 Add Two Numbers
关注微信公众号:CodingTechWork,一起学习进步. 题目 Add Two Numbers: You are given two non-empty linked lists represen ...
- leetcode(二)—— Add Two Numbers(Python/C++)
Add Two Numbers | LeetCode OJ 逻辑是简单的,主要是处理进位,以及两序列长度不一致的情况: Python # 链表结点定义 class ListNode(object):d ...
- LeetCode题解:Add Two Numbers
LeetCode题解 说明:本人不是什么算法高手,部分内容参考了Google.stackoverflow.segmentfault中得到的一些解答或者代码.之所以去做Leetcode上的题是因为毕业工 ...
- LeetCode 445. Add Two Numbers II
LeetCode 445. Add Two Numbers II Solution1:我的答案 利用了栈,这样就不用翻转链表了... /*** Definition for singly-linked ...
- LeetCode久不久来一题系列之Add Two Numbers
题目来源: https://leetcode.com/problems/add-two-numbers/description/ 题目: Add Two Numbers 题目描述: You are g ...
- LeetCode刷题实战(2):Add Two Numbers
题2描述: 2 Add Two Numbers 29.10% Medium You are given two non-empty linked lists representing two non- ...
- 【注意】LeetCode 2. Add Two Numbers
LeetCode 2. Add Two Numbers 这种沙比提怎么都写不对了??? Solution1:学习这种写法 /*** Definition for singly-linked list. ...
最新文章
- 【143】360云盘资源
- Twitter新计划:审视自家算法里的“无心之失”
- DevExpress WinFormsSuite 本地化(Simplified Chinese OR Traditional Chinese)
- 图片怎么等比缩放_图片300kb,50kb压缩【方法笔记】
- android 获取手机SD卡和手机的内部存储
- javascript --- 抽象相等
- [推荐]大量 Blazor 学习资源(一)
- apache shiro_Apache Shiro:简化应用程序安全性
- 将CSV文件存为HTML文件形式
- Redis 笔记之 Java 操作 Redis(Jedis)
- 畅捷通魔方MP70初体验
- 11083 旅游背包(优先做)
- pycharm画图出现Font family [‘Micro- soft YaHei‘] not found. Falling back to DejaVu Sans.的错误
- 数字化转型不给力?你可能少了vPro这样的“芯”动力
- 华硕路由 ac ax_【路由器】华硕网络开启802.11ax电竞生态
- 计算机信息心得体会作文50字,考试感想作文50字5篇
- RK3588 camera2 支持4K录像
- 8款受欢迎的代码编辑器,你喜欢用哪一款呢?
- PLC_SCL数据查找算法
- 如何用excel做一份好看的年度业绩报告,让领导满意呢?
热门文章
- linux实现设备在uboot状态下利用TFTP和NFS加载内核镜像和根文件系统
- SHELL脚本项目实例(一)
- POS-商户手续费-从生活剖析,通俗易懂
- SQL Server 2012 T-SQL 新特性
- 15-struct(构造函数,重载)
- HDU 5980 位运算 进制转换函数 DEV C++调试失败的原因
- 使用web.xml方式加载Spring时,获取Spring context的两种方式
- CentOS下MYSQL数据库的安装
- Android Studio 设置/更改 SDK 路径
- 【转】修改UCGUI源代码以支持多个独立窗体的说明