LeetCode Algorithm 02_Add Two Numbers
You are given two linked lists representing two non-negative 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
Tags:Linked List, Math
分析:逐位相加,考虑进位。
/**
* 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) {
if (!l1 || !l2) {
return NULL;
}
int carry = ; //进位
ListNode * result = new ListNode();
ListNode * first = result; //追踪结果链表的当前节点
ListNode * pre = NULL; //追踪结果链表的前一个节点
//当l1和l2均没有超过链表末尾节点时
while (l1 && l2) {
first->val = (carry + l1->val + l2->val) % ;
carry = (carry + l1->val + l2->val) / ;
if (pre == NULL)
pre = first;
else {
pre->next = first;
pre = first;
}
first = new ListNode();
l1 = l1->next;
l2 = l2->next;
}
//当l1和l2都超过链表末尾节点时
if (!l1 && !l2) {
if (carry == ) {
first->val = carry;
pre->next = first;
}
return result;
}
//当l1超过末尾而l2尚未超过时
if (!l1 && l2) {
while (l2) {
first->val = (carry + l2->val) % ;
carry = (carry + l2->val) / ;
if (pre == NULL)
pre = first;
else {
pre->next = first;
pre = first;
}
first = new ListNode();
l2 = l2->next;
}
if (carry == ) {
first->val = ;
pre->next = first;
}
return result;
}
//当l2超过末尾而l1尚未超过时
if (!l2 && l1) {
while (l1) {
first->val = (carry + l1->val) % ;
carry = (carry + l1->val) / ;
if (pre == NULL)
pre = first;
else {
pre->next = first;
pre = first;
}
first = new ListNode();
l1 = l1->next;
}
if (carry == ) {
first->val = ;
pre->next = first;
}
return result;
} }
};
LeetCode Algorithm 02_Add Two Numbers的更多相关文章
- LeetCode Algorithm
LeetCode Algorithm 原文出处:[LeetCode] 算法参考:[陈皓 coolshell] 1. Two Sum 3. Longest Substring Without Repea ...
- 【LeetCode】386. Lexicographical Numbers 解题报告(Python)
[LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- LeetCode:1. Add Two Numbers
题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...
- LeetCode Algorithm 05_Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Bitwise AND of Numbers Range 数字范围位相与
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- LeetCode 2 Add Two Numbers 模拟,读题 难度:0
https://leetcode.com/problems/add-two-numbers/ You are given two linked lists representing two non-n ...
- LeetCode 2 Add Two Numbers(链表操作)
题目来源:https://leetcode.com/problems/add-two-numbers/ You are given two linked lists representing two ...
随机推荐
- LRJ入门经典-0903切蛋糕305
原题 LRJ入门经典-0903切蛋糕305 难度级别:B: 运行时间限制:1000ms: 运行空间限制:256000KB: 代码长度限制:2000000B 试题描述 如图所示有一个矩形蛋糕,上面划分成 ...
- redis练习手册<二>快速入门
Redis是一个开源,先进的key-value存储,并用于构建高性能,可扩展的Web应用程序的完美解决方案. Redis从它的许多竞争继承来的三个主要特点: Redis数据库完全在内存中,使用磁盘仅用 ...
- HttpComponents入门解析
1 简介 超文本传输协议(http)是目前互联网上极其普遍的传输协议,它为构建功能丰富,绚丽多彩的网页提供了强大的支持.构建一个网站,通常无需直接操作http协议,目前流行的WEB框架已经透明的将这些 ...
- 洛谷 P3486 [POI2009]KON-Ticket Inspector
P3486 [POI2009]KON-Ticket Inspector 题目描述 Byteasar works as a ticket inspector in a Byteotian Nationa ...
- thinkphp中 Illegal offset type异常
thinkphp中 Illegal offset type异常 一.错误提示 二.解决思路 1.看出错提示中的函数为assign函数,那说明是我们在从控制器assign数据到页面的部分出现了错误 2. ...
- 洛谷P1908 逆序对(归并排序)
题目描述 猫猫TOM和小老鼠JERRY最近又较量上了,但是毕竟都是成年人,他们已经不喜欢再玩那种你追我赶的游戏,现在他们喜欢玩统计.最近,TOM老猫查阅到一个人类称之为“逆序对”的东西,这东西是这样定 ...
- 【习题 8-9 1613】 K-Graph Oddity
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 感觉最大度数|1就是最多需要的个数了. 就贪心一下. 然后模拟染色的过程就可以了. (贪心染色就可以了 (看看周围哪个颜色没有,就用 ...
- STL_算法_查找算法(search、find_end)
C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) search //从左往右找第一个符合条件的子区间 全部容器适用 find_end //从右往左找 ...
- Service-监听手机来电
public class MonitorPhone extends Activity { TelephonyManager tManager; @Override protected void onC ...
- “此文件来自其他计算机,可能被阻止以帮助保护该计算机” 教你win7解除阻止程序运行怎么操作
win7 批量解除可执行文件的锁定 "此文件来自其他计算机,可能被阻止以帮助保护该计算机" http://blog.csdn.net/gscsnm/article/details/ ...