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的更多相关文章

  1. LeetCode Algorithm

    LeetCode Algorithm 原文出处:[LeetCode] 算法参考:[陈皓 coolshell] 1. Two Sum 3. Longest Substring Without Repea ...

  2. 【LeetCode】386. Lexicographical Numbers 解题报告(Python)

    [LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  3. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  4. LeetCode:1. Add Two Numbers

    题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...

  5. LeetCode Algorithm 05_Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  6. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  7. [LeetCode] Bitwise AND of Numbers Range 数字范围位相与

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  8. LeetCode 2 Add Two Numbers 模拟,读题 难度:0

    https://leetcode.com/problems/add-two-numbers/ You are given two linked lists representing two non-n ...

  9. LeetCode 2 Add Two Numbers(链表操作)

    题目来源:https://leetcode.com/problems/add-two-numbers/ You are given two linked lists representing two ...

随机推荐

  1. 【hdu 1429】胜利大逃亡(续)

    [Link]: [Description] 给你一个n*m的格子; 里面有钥匙,以及钥匙能开的门; 以及墙,以及起点,以及出口; 问你从起点出发,到出口的话,能不能在t时间内到; [Solution] ...

  2. qqwry - 纯真ip库的golang服务

    qqwry 纯真 IP 库的一个服务.通过http提供一个ip地址归属地查询支持 软件介绍 我们大家做网站的时候,都会需要将用户的IP地址转换为归属地址功能,而之前的作法大都是从硬盘的数据文件中读取, ...

  3. jmeter名词解释之聚合报告

    新浪围脖>@o蜗牛快跑o    温馨提示: 1. tps(吞吐量)表征系统性能,系统的好坏能够用这个评估 2. 90%Line是满足需求响应时间的重要指标,假设用户需求说是响应时间不小于5s,那 ...

  4. 消灭星星的数组高效率算法(c++代码,控制台程序)

    #include <iostream> using namespace std; #define ROW 12 #define COL 10 class Star { public: en ...

  5. Docs-->.NET-->API reference-->System.​Web.​UI-->Control-->Methods-->Find​Control

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.control.findcontrol?view=netframework-4.7 ...

  6. Springboot优化

    https://www.cnblogs.com/chen110xi/p/6198481.html

  7. 【Django】ContentType组件

    目录 理解 表结构 使用 @ 好,现在我们有这样一个需求,我们的商城里有很多的商品,然而节日要来了,我们要搞活动. 那么,我们就要设计优惠券,优惠券都有什么类型呢?满减的.折扣的.立减的.等等等... ...

  8. [Python] Python's namedtuples can be a great alternative to defining a class manually

    # Why Python is Great: Namedtuples # Using namedtuple is way shorter than # defining a class manuall ...

  9. VS:&quot;64位调试操作花费的时间比预期要长&quot;的一解决途径

    解决的方法之中的一个: 在命令提示符那里打入例如以下命令: netsh winsock reset catalog netsh int ip reset reset.log hit 重新启动电脑后,就 ...

  10. A. Keyboard Codeforces Round #271(div2)

    A. Keyboard time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...