You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in forward order, such that the 1's digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.

 
Example

Given 6->1->7 + 2->9->5. That is, 617 + 295.

Return 9->1->2. That is, 912.

解法一:

 class Solution {
public:
/**
* @param l1: the first list
* @param l2: the second list
* @return: the sum list of l1 and l2
*/
ListNode *addLists2(ListNode *l1, ListNode *l2) {
reverse(l1);
reverse(l2);
ListNode *dummy = new ListNode();
ListNode *tail = dummy;
int carry = ; while (l1 != NULL || l2 != NULL) {
int sum = carry;
if (l1 != NULL) {
sum += l1->val;
l1 = l1->next;
}
if (l2 != NULL) {
sum += l2->val;
l2 = l2->next;
}
if (sum > ) {
carry = ;
sum -= ;
} else {
carry = ;
}
tail->next = new ListNode(sum);
tail = tail->next;
} if (carry == ) {
tail->next = new ListNode();
tail = tail->next;
} reverse(dummy->next); return dummy->next;
} void reverse(ListNode *&head) {
ListNode *prev = NULL; while (head != NULL) {
ListNode *temp = head->next;
head->next = prev;
prev = head;
head = temp;
} head = prev;
}
};

链表先翻转,再求和,然后再翻转

解法二:

 public class Solution {
/**
* @param l1: the first list
* @param l2: the second list
* @return: the sum list of l1 and l2
*/
public ListNode addLists2(ListNode l1, ListNode l2) {
Stack<Integer> temp1 = reverseNode(l1);
Stack<Integer> temp2 = reverseNode(l2); ListNode point = new ListNode(0);
int flag = 0; while((!temp1.isEmpty()) && (!temp2.isEmpty())){
int value = temp1.pop() + temp2.pop() + flag;
flag = value / 10;
value = value % 10;
ListNode cur = new ListNode(value);
cur.next = point.next;
point.next = cur;
} while(!temp1.isEmpty()){
int value = temp1.pop() + flag;
flag = value / 10;
value = value % 10;
ListNode cur = new ListNode(value);
cur.next = point.next;
point.next = cur;
} while(!temp2.isEmpty()){
int value = temp2.pop() + flag;
flag = value / 10;
value = value % 10;
ListNode cur = new ListNode(value);
cur.next = point.next;
point.next = cur;
} if(flag == 1){
ListNode cur = new ListNode(1);
cur.next = point.next;
point.next = cur;
} return point.next;
} public Stack<Integer> reverseNode(ListNode temp){
Stack<Integer> record = new Stack<Integer>(); while(temp != null){
record.push(temp.val);
temp = temp.next;
} return record;
}
}

利用栈的先进后出,记录两个链表的节点值。然后计算对应位置的两个数字之和,如果有进位,赋值给flag并带入下一个计算。

参考@sunday0904 的代码

221. Add Two Numbers II【medium】的更多相关文章

  1. 445. Add Two Numbers II【Medium】【两个链表求和】

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

  2. 82. Remove Duplicates from Sorted List II【Medium】

    82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...

  3. 92. Reverse Linked List II【Medium】

    92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...

  4. 18. Subsets II【medium】

    Given a list of numbers that may has duplicate numbers, return all possible subsets Notice Each elem ...

  5. 150. Best Time to Buy and Sell Stock II【medium】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  6. 2. Add Two Numbers【medium】

    2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...

  7. LeetCode:课程表II【210】

    LeetCode:课程表II[210] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一 ...

  8. LeetCode:路径总和II【113】

    LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...

  9. LeetCode:组合总数II【40】

    LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...

随机推荐

  1. js禁止复制粘贴

    1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键,其实是禁止快捷菜单,因为不光右键可以弹出这个菜单,键盘上空格 ...

  2. Hive 内建操作符与函数开发——深入浅出学Hive

    第一部分:关系运算 Hive支持的关系运算符 •常见的关系运算符 •等值比较: = •不等值比较: <> •小于比较: < •小于等于比较: <= •大于比较: > •大 ...

  3. jquery dialog close icon missing 关闭图片丢失,样式丢失问题

    http://stackoverflow.com/questions/17367736/jquery-ui-dialog-missing-close-icon

  4. Linux中文乱码问题终极解决方法

    方法一: 修改/root/.bash_profile文件,增加export LANG=zh_CN.GB18030该文件在用户目录下,对于其他用户,也必须相应修改该文件. 使用该方法时putty能显示中 ...

  5. JDK 7中的函数式编程思想[转载]

    原文作者的观点是Lambda表达式一定会包含在JDK 7中,而全文也着重介绍了这方面的知识,作者认为函数式编程的概念也将出现在JDK 7中. Lambda表达式 Lambda表达式并不是什么新概念,自 ...

  6. 在做了 BasePage 时: 只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态。还请确保在应用程序配置的 / / 节中包括

    摘自: http://lichengguizy.blog.163.com/blog/static/11771858620122342749552/ 只有在配置文件或 Page 指令中将 enableS ...

  7. RocketMQ-创建MappedFile本地文件

    了解RocketMQ的都知道,它会保存所有的消息到本地文件.这个文件就是 MappedFile,每一个文件对应一个MappedFile.默认情况下大小位1g. 在MessageStoreConfig中 ...

  8. Mac OS X 10.10 执行 Eclipse 提示须要安装 Java

  9. 【转】Android一些知识点汇总

    Android常用知识点总汇 一.系统上安装了多种浏览器,能否指定某浏览器访问指定页面?请说明原由. 如果在你的android系统上安装了多种浏览器,能否指定某浏览器访问指定页面?答案当然是:肯定的. ...

  10. RocketMq通信协议格式及编解码 (源码分析)

    一.RocketMq broker服务器与客户端的网络通信是基于netty4.x实现的,重点分析  RocketMq设计的通信协议及对应的编解码 开发.         名字解释           ...