221. Add Two Numbers II【medium】
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.
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】的更多相关文章
- 445. Add Two Numbers II【Medium】【两个链表求和】
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
- 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 ...
- 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 ...
- 18. Subsets II【medium】
Given a list of numbers that may has duplicate numbers, return all possible subsets Notice Each elem ...
- 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 ...
- 2. Add Two Numbers【medium】
2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...
- LeetCode:课程表II【210】
LeetCode:课程表II[210] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一 ...
- LeetCode:路径总和II【113】
LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...
- LeetCode:组合总数II【40】
LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...
随机推荐
- phaser框架制作游戏的例子,加上自己的注释
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 三种常见的部署Kubernetes的方式
三种常见的部署Kubernetes的方式 嘹亮的小号 Ghostcloud-001工号,资深Docker玩家,分布式系统研发11年. 关注他 容器技术将应用程序及其依赖关系与操作系统进行分离,不 ...
- Jpeglib读取jpg文件 【转】
http://blog.csdn.net/blues1021/article/details/45424695 整理自 : http://hi.baidu.com/lewutian/item/e8ee ...
- Maven+SpringMVC+Freemarker入门Demo
1 参考http://blog.csdn.net/haishu_zheng/article/details/51490299,用第二种方法创建一个名为mavenspringmvcfreemarker的 ...
- JDBC纯驱动方式连接MySQL
1 新建一个名为MysqlDemo的JavaProject 2 从http://dev.mysql.com/downloads/connector/j/中下载最新的驱动包. 这里有.tar.gz和.z ...
- iOS 中 JSON 数据交换格式
JSON (JavaScript Object Notation)是一种轻量级的数据交换格式. JSON 的详细教程,能够參见 JSON 中国:http://www.json.org.cn/ ...
- Unity5.1 新的网络引擎UNET(九) UNET 官方推荐视频教程
孙广东 2015.7.14 在新的网络引擎出现之前,Unity提供的是 内置 Raknet网络引擎, 这一次Unity想更新UGUI一样,花了大的手笔更新了, UNET. 原来的旧的网络组件 被提示 ...
- java程序main方法的参数String[] args
public class ArgsTest { public static void main(String[] args) { System.out.println(args.length); fo ...
- 设计模式 - 迭代器模式(iterator pattern) 具体解释
迭代器模式(iterator pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 迭代器模式(iterator pattern) : 提供一 ...
- STL源代码剖析 容器 stl_map.h
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie map ------------------------------------------ ...