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 ...
随机推荐
- Swift学习笔记(5)--数组
数组的下标从0开始计数,相关方法属性涉及到下标时也从0开始计数 1.定义: //1.可变数组 var cityArray = ["Portland","San Franc ...
- CSUOJ 1644 超能陆战队
1644: 超能陆战队 Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 6 Solved: 1[Submit][Status][Web Board] D ...
- Spring Cloud学习笔记【二】Eureka 服务提供者/服务消费者(ribbon)
Ribbon 是 Netflix 发布的开源项目,主要功能是为 REST 客户端实现负载均衡.它主要包括六个组件: ServerList,负载均衡使用的服务器列表.这个列表会缓存在负载均衡器中,并定期 ...
- 9.9递归和动态规划(九)——N皇后
/** * 功能:打印八皇后在8*8棋盘上的各种摆法.当中每一个皇后都不同行.不同列,也不在对角线上. * 这里的"对角线"指的是全部的对角线,不仅仅是平分整个棋盘的那两条对角 ...
- 指尖上的电商---(10)SolrAdmin中加入多核
在Solr中有的时候,我们并不仅仅是须要一种形式的索引文件.可能须要多种不同数据的索引文件.这时我们就能够在同一个Solr以下创建 多核. 比方,我们在solr以下想把产品信息和分类信息各存放一个索引 ...
- 基于TI Davinci架构的多核/双核开发高速扫盲(以OMAP L138为例),dm8168多核开发參考以及达芬奇系列资料user guide整理
基于TI Davinci架构的双核嵌入式应用处理器OMAPL138开发入门 原文转自http://blog.csdn.net/wangpengqi/article/details/8115614 感谢 ...
- java 链接server上的 mongodb 出现 connect time out 问题
异常信息 十二月 22, 2014 5:27:58 下午 com.mongodb.DBTCPConnector initDirectConnection 警告: Exception executing ...
- button-xml 中android:clickable="false" 属性
今天在做项目的时候,遇到了一个问题,就是需要把一个常按监听事件,加到一个linearlayout中,但是,这个linearlayout中有其他的button.textview等控件,这样就导致当我们常 ...
- .Net写的比较清晰的接口
尼玛,隔行如隔山. .Net真操蛋. /// <summary> /// 加入群 /// </summary> /// <returns></returns& ...
- es7 --- 新特性
ES7只有2个特性: includes() 指数操作符 不使用ES7 使用indexOf()验证数组中是否存在某个元素,这时需要根据返回值是否为-1来判断: let arr = ['react', ' ...