【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. SqlServer Base64解码中文

    最近在做一个和拥有TurboCRM相关的项目,其中需要取出客户信息的联系人字段,经过查看,这个字段在存入时用Base64加密过了 这个功能在应用层实现是很方便的,但是由于一些特殊原因,只能放到SqlS ...

  2. windows安装zookeeper单机版

    1.在apache的官方网站提供了好多镜像下载地址,然后找到对应的版本,目前最新的是3.4.6下载地址:http://mirrors.cnnic.cn/apache/zookeeper/zookeep ...

  3. Get just enough boost voltage - current-mirror circuit - VOUT tracks VIN varies

    Adding a current-mirror circuit to a typical boost circuit allows you to select the amount of boost ...

  4. python笔记21-列表生成式

    前言 python里面[]表示一个列表,快速生成一个列表可以用range()函数来生成. 对列表里面的数据进行运算和操作,生成新的列表最高效快速的办法,那就是列表生成式了. range() 1.一个连 ...

  5. pytest文档8-html报告报错截图+失败重跑

    前言 做web自动化的小伙伴应该都希望在html报告中展示失败后的截图,提升报告的档次,pytest-html也可以生成带截图的报告. conftest.py 1.失败截图可以写到conftest.p ...

  6. C语言中常用计时方法总结

    转自:http://blog.csdn.net/fz_ywj/article/details/8109368 C语言中常用计时方法总结 1. time() 头文件:time.h 函数原型:time_t ...

  7. JAVA 文本 TXT 操作工具类

    import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; imp ...

  8. 如何安装pip、升级pip包。mac下安装包的路径

    参考:https://pip.pypa.io/en/stable/installing/ 1.wget -c  https://bootstrap.pypa.io/get-pip.py 2.pytho ...

  9. Nginx发展现状及未来特性

    Nginx ("engine x")是一个高性能的HTTP和反向代理 服务器,也是一个IMAP/POP3/SMTP 代理服务器,其特点是占用内存少,并发能力强.到目前完为止,Ngi ...

  10. Shell学习:grep, sed, awk命令的练习题

    http://www.cnblogs.com/chengmo/archive/2013/01/17/2865479.html 文件:datafileSteve Blenheim:238-923-736 ...