题目


You are given two non-empty linked lists representing two non-negative integers. 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.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example:

 Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)

 Output: 7 -> 0 -> 8

 Explanation: 342 + 465 = 807.

思路


主要是考察链表的求和,由于链表中存放的是逆序的数字,所以两个数的个位数已经对齐,不用考虑对齐问题。其次要考虑到进位的问题:

  • 当前位数字:sum %10
  • 进位:sum / 10

C++

ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {

    ListNode* result = new ListNode(-1);
ListNode* aux = result; //辅助,用来处理中间链表
int carry = 0;
while(l1 != nullptr || l2 != nullptr || carry != 0){ //这种方法使得当其中一个链表为空时,也能作加法,从而能够同时对两个链表进行循环
int a = l1 ? l1->val : 0; //判断链表当前是否为空,如果为空,则值为0
int b = l2 ? l2->val : 0; int sum = a + b + carry;
carry = sum / 10; //进位用取模来获得
aux->next = new ListNode(sum%10); //通过取模消除进位的影响
aux = aux ->next;
if(l1 != nullptr )
l1 = l1->next;
if(l2 != nullptr )
l2 = l2->next;
} return result->next;
}

Python

def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
result = ListNode(-1)
aux = result
carry = 0
while(l1 or l2 or carry):
a = l1 and l1.val or 0
b = l2 and l2.val or 0
sum = a + b + carry
carry = sum / 10
aux.next = ListNode (sum % 10)
aux = aux.next
if(l1):
l1 = l1.next
if(l2):
l2 = l2.next return result.next

2. Add Two Numbers[M]两数相加的更多相关文章

  1. 445 Add Two Numbers II 两数相加 II

    给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表.你可以假设除了数字 0 之外,这两个数字都不会以零开头.进阶:如果输入链表 ...

  2. [leetcode]445. Add Two Numbers II 两数相加II

    You are given two non-empty linked lists representing two non-negative integers. The most significan ...

  3. LeetCode 445. Add Two Numbers II (两数相加 II)

    题目标签:Linked List 题目给了我们两个 数字的linked list,让我们把它们相加,返回一个新的linked list. 因为题目要求不能 reverse,可以把 两个list 的数字 ...

  4. [CareerCup] 18.1 Add Two Numbers 两数相加

    18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...

  5. [Swift]LeetCode2. 两数相加 | Add Two Numbers

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  6. [Swift]LeetCode445. 两数相加 II | Add Two Numbers II

    You are given two non-empty linked lists representing two non-negative integers. The most significan ...

  7. LeetCode 445. 两数相加 II(Add Two Numbers II)

    445. 两数相加 II 445. Add Two Numbers II 题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个 ...

  8. LeetCode 2. 两数相加(Add Two Numbers)

    2. 两数相加 2. Add Two Numbers 题目描述 You are given two non-empty linked lists representing two non-negati ...

  9. LeetCode 2. 两数相加(Add Two Numbers)

    题目描述 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入: ...

随机推荐

  1. AJAX异步删除操作

    @Ajax.ActionLink("删除", "Delete", new {id = user.Id}, ajaxOption) @{ var ajaxOpti ...

  2. Linux od与hexdump命令

    od命令:以指定格式输出文件内容常用格式:od -Ax -tx1 filename直接格式:od filename 等价 od -o filename语法:od [-abcdfsiloxv] [-An ...

  3. 基于 Web 的 Go 语言 IDE - Wide 1.4.0 发布!

    Wide 是什么 Wide 是一个基于 Web 的 Go 语言团队 IDE . 在线开发:打开浏览器就可以进行开发.全快捷键 智能提示:代码自动完成.查看表达式.编译反馈. Lint 实时运行:极速编 ...

  4. 【SQL】分析函数功能-排序

    1:排名,不考虑并列问题 row_number() 2:排名,有并列,并列后的排名不连续 rank() 3:排名,有并列,并列后的排名连续 dense_rank() 测试: SQL> creat ...

  5. Ubuntu win8 小设备版本

    Ubuntu小设备支持列表:https://wiki.ubuntu.com/Touch/Devices win8 小设备 刷ubutntu:tieba.baidu.com/p/2772275438

  6. PythonGIS可视化—Matplot basemap工具箱

    原文链接:http://www.douban.com/group/topic/32821988/ 原文链接:http://www.cnblogs.com/vamei/archive/2012/09/1 ...

  7. 【seo】title / robots / description / canonical

    1.title title,就是浏览器上显示的那些内容,不仅用户能看到,也能被搜索引擎检索到(搜索引擎在抓取网页时,最先读取的就是网页标题,所以title是否正确设置极其重要. 1)title一般不超 ...

  8. 应用一:Vue之开发环境搭建

    简单分享下vue项目的开发环境搭建流程~ 1.安装nodeJS vue的运行是要依赖于node的npm的管理工具来实现,下载地址:https://nodejs.org/en/.安装完成之后以管理员身份 ...

  9. Go 语言一本通

    什么是GO语言? Go 是一个开源的编程语言,它能让构造简单.可靠且高效的软件变得容易. Go是从2007年末由Robert Griesemer, Rob Pike, Ken Thompson主持开发 ...

  10. 路飞学城Python-Day100

    Django项目之图书馆项目 1.项目架构 2.表结构设计 from django.db import models # Create your models here. #作者详情表 class A ...