【Problem:2-Add Two Numbers

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

【Solution】

1)From 九章算法:(can run, but "Submission Result: Wrong Answer!")

 class Solution:
def addTwoNumbers(self, l1, l2):
head = ListNode(0)
ptr = head
carry = 0
while True:
if l1 != None:
carry += l1.val
l1 = l1.next
if l2 != None:
carry += l2.val
l2 = l2.next
ptr.val = carry % 10
carry /= 10
# 运算未结束新建一个节点用于储存答案,否则退出循环
if l1 != None or l2 != None or carry != 0:
ptr.next = ListNode(0)
ptr = ptr.next
else:
break
return head

2)Java :

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if(l1 == null && l2 == null) {
return null;
} ListNode head = new ListNode(0);
ListNode point = head;
int carry = 0;
while(l1 != null && l2!=null){
int sum = carry + l1.val + l2.val;
point.next = new ListNode(sum % 10);
carry = sum / 10;
l1 = l1.next;
l2 = l2.next;
point = point.next;
} while(l1 != null) {
int sum = carry + l1.val;
point.next = new ListNode(sum % 10);
carry = sum /10;
l1 = l1.next;
point = point.next;
} while(l2 != null) {
int sum = carry + l2.val;
point.next = new ListNode(sum % 10);
carry = sum /10;
l2 = l2.next;
point = point.next;
} if (carry != 0) {
point.next = new ListNode(carry);
}
return head.next;
}
} // version: 高频题班
public class Solution {
/**
* @param l1: the first list
* @param l2: the second list
* @return: the sum list of l1 and l2
*/
public ListNode addLists(ListNode l1, ListNode l2) {
// write your code here
ListNode dummy = new ListNode(0);
ListNode tail = dummy; int carry = 0;
for (ListNode i = l1, j = l2; i != null || j != null; ) {
int sum = carry;
sum += (i != null) ? i.val : 0;
sum += (j != null) ? j.val : 0; tail.next = new ListNode(sum % 10);
tail = tail.next; carry = sum / 10;
i = (i == null) ? i : i.next;
j = (j == null) ? j : j.next;
} if (carry != 0) {
tail.next = new ListNode(carry);
}
return dummy.next;
}
}

3)C++:yes, accepted

/**
* 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) {
// 题意可以认为是实现高精度加法
ListNode *head = new ListNode();
ListNode *ptr = head;
int carry = ;
while (true) {
if (l1 != NULL) {
carry += l1->val;
l1 = l1->next;
}
if (l2 != NULL) {
carry += l2->val;
l2 = l2->next;
}
ptr->val = carry % ;
carry /= ;
// 当两个表非空或者仍有进位时需要继续运算,否则退出循环
if (l1 != NULL || l2 != NULL || carry != ) {
ptr = (ptr->next = new ListNode());
} else break;
}
return head;
}
};

【References】

1、http://www.jiuzhang.com/solution/add-two-numbers/

Time complexity:O(n^2)2

LeetCode-2: Add Two Numbers的更多相关文章

  1. LeetCode OJ:Add Two Numbers (相加链表之数)

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  2. leetcode 第二题Add Two Numbers java

    链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...

  3. # Leetcode 67:Add Binary(二进制求和)

    Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...

  4. 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...

  5. 《LeetBook》LeetCode题解(2):Add Two Numbers [M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  6. C# 写 LeetCode Medium #2 Add Two Numbers

    2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...

  7. Q2:Add Two Numbers

    2. Add Two Numbers 官方的链接:2. Add Two Numbers Description : You are given two non-empty linked lists r ...

  8. LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表

    题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...

  9. leetcode:Add Two Numbers

    题目描述:You are given two linked lists representing two non-negative numbers. The digits are stored in ...

随机推荐

  1. POJ 3237 Tree (树链剖分)

    Tree Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 2825   Accepted: 769 Description ...

  2. 卸载oracle数据库

    Windows Registry Editor Version 5.00 [-HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\OracleOr ...

  3. Linux系统下查看目录大小

    转载:http://blog.csdn.net/iamlaosong/article/details/7085178 我们有个系统每天要下载数据,为了检查下载数据是否正常,需要查看下载数据目录大小,因 ...

  4. VisualStudio:WEB 性能测试和负载测试 入门

    背景 一直做的是中小企业应用,很少关注性能和负载这里,进来准备看一本关于并发编程的图书,为了量化的测试 WEB 环境的性能和负载,特意玩了一下 VS 提供的测试项目. 新的测试项目 新建项目 性能测试 ...

  5. Tomcat:基础安装和使用教程

    背景 此文记录了 Tomcat 的基本使用方法,主要为了强化记忆. 安装步骤 第一步:下载和安装 Java 下载地址:http://www.oracle.com/technetwork/java/ja ...

  6. Spring3.0.5jar包用法详解 [转载]

    Spring3.X以后jar包进行了重构,取消了原来2.X版本中的总的spring.jar包,而是把总包中的功能全部分开打包.正在向osgi靠拢. 各个jar包详解如下: 1. org.springf ...

  7. [Linux] git send-email的使用

    1. git send-email is included in an individual package, named "git-email":$ sudo apt-get i ...

  8. Python学习(九)IO 编程 —— 文件夹及文件操作

    Python 文件夹及文件操作 我们经常会与文件和目录打交道,对于这些操作,python可以使用 os 及 shutill 模块,其中包含了很多操作文件和目录的函数. os 可以执行简单的文件夹及文件 ...

  9. Minify压缩JS和CSS

    Minify把CSS和JS压缩和削减(Minify:去掉空格回车符等),以及把多个CSS,JS文件整合到一个文件里.不要以为你的大带宽没有必要进行这类优化.使用它的理由更重要的是文件合并,而不是压缩, ...

  10. c# 使用api函数 ShowWindowAsync 控制窗体

    1.需要匯入 System.Runtime.InteropServices 命名空間 2.宣告 ShowWindowAsync 函數 [DllImport("user32.dll" ...