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 的代码

最新文章

  1. [Erlang 0105] Erlang Resources 小站 2013年1月~6月资讯合集
  2. http://www.360doc.com/content/13/0516/22/12094763_285956121.shtml
  3. Linux下动态库查找路径的问题
  4. JEECMS v8 发布,java 开源 CMS 系统
  5. 用wireshark抓包分析TCP三次握手、四次挥手以及TCP实现可靠传输的机制
  6. UIButton之Block回调
  7. Entity Framework 之三层架构
  8. Java Web 前端高性能优化(二)
  9. 【Mysql学习笔记】浅析mysql的binlog
  10. 大型网站的架构设计问题&mdash;-大型高并发高负载网站的系
  11. (使用步骤)ThinkPHP3.1.2中如何配置Ckeditor_4.1.1和Ckfindtor(转)
  12. 开源欣赏wordpress之post.php
  13. Multiscale Combinatorial Grouping 学习和理解源代码(一)
  14. UVA 10911 Forming Quiz Teams(dp + 集合最优配对问题)
  15. docker~学习笔记索引
  16. Numpy 系列(七)- 常用函数
  17. Unity用GUI绘制Debug/print窗口/控制台-打包后测试
  18. CMake for MFC example
  19. ssh面试题总结
  20. cocos2d-x开发记录:一,搭建环境

热门文章

  1. 使用git bash 代替cmd
  2. asp.net core 1.0初识
  3. JBoss入门
  4. iOS:CYLTabBarController的具体使用实例:实现新浪微博的主流框架
  5. [Todo]Redis &amp; Mysql可以看的书籍
  6. WordPress &lt; 3.6.1 PHP 对象注入漏洞
  7. 什么是JSONP?
  8. 解决:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compile
  9. 网页HTML代码:滚动文字的制作
  10. 解决rails4.0中send_file文件下载两次的问题