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. 紫书 例题 9-13 UVa 1220 (最大独立子集)

    这里的状态定义的非常的巧妙,d(i, 1)表示以i为根节点且选i的子树的最大独立子集 d(i, 0)表示以i为根节点且不选i的子树的最大独立子集 d(i, 1) = sum{ d(v, 0) | v是 ...

  2. 【Uva 1629】 Cake slicing

    [Link]: [Description] 给你一个n*m的格子; 然后里面零零散散地放着葡萄 让你把它切成若干个小矩形方格 使得每个小矩形方格都恰好包含有一个葡萄. 要求切的长度最短; 问最短的切割 ...

  3. snmpd修改端口

    http://blog.csdn.net/cau99/article/details/5077239 http://blog.csdn.net/gua___gua/article/details/48 ...

  4. js实现table排序(jQuery下的jquery.sortElements)

    项目中要实现table排序的功能. 网上有非常多解决方式,非常多都基于jQuery. jquery.tablesorter.大小17KB.只是他的首页在ie10下兼容性有点问题. DataTables ...

  5. ognl.OgnlException: target is null for setProperty(null,&quot;XXXX&quot;...)

    今天遇到了这个奇葩问题,最后来回比对了一下前辈写过的一段完整代码后才发现问题. Error大概描写叙述为: 警告: Error setting expression 'XXX' with value ...

  6. OSX: 禁止iCloud钥匙链?

    自从10.9有了一个新的功能叫viewlocale=zh_CN">iCloud钥匙串的,就出现了不少的麻烦.一是在10.9.3之前.好多人出现无限循环地要求用户输入Local item ...

  7. c# List集合学习

    1---集合,可以理解成容器 泛型集合 非泛型集合2---使用集合用到的命名空间 using System.Collections.Generic;3---集合是如何来的?集合的前辈是数组,数组在内存 ...

  8. BZOJ5332: [Sdoi2018]旧试题(莫比乌斯反演)

    时光匆匆,转眼间又是一年寒暑…… 这是小 Q 同学第二次参加省队选拔赛. 今年,小 Q 痛定思痛,不再冒险偷取试题,而是通过练习旧 试题提升个人实力.可是旧试题太多了,小 Q 没日没夜地做题,却看不到 ...

  9. 谈一谈Nginx的强大

    什么是Nginx? Nginx是一款高性能,开源的,支持高并发而轻量级的Web服务器,同时也是具有反向代理服务器及电子邮件(IMAP/POP3)的代理服务器. 基于REST架构风格,并且以统一资源描述 ...

  10. 免费超大量邮件发送服务Amazon SES和Mailgun提供SMTP和API支持

    一般来说网站注册.论坛消息.新闻推送.广告宣传等都会有发送邮件服务,大量的邮件发送服务如果用PHP来发送,一是会消耗主机资源,二是容易被各大邮箱判定为垃圾邮件而被拒收.用第三方的邮局服务发送邮件,可以 ...