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 ...
随机推荐
- minishift的本地代码构建
看文档说支持,但实际尝试了下,失败 发现仍然是找github中的代码 找了半天,发现是在目录下有.git的隐含目录,然后copy到其他目录下删除,然后再试,发现仍然失败. 日志看是指定的目录并没有传入 ...
- sql server触发器复制记录
Create Trigger test_tri5 on test after insert as begin declare @id sysname, @tel sysname, @name sysn ...
- Spark学习视频整合
1.<Scala深入浅出实战经典>http://pan.baidu.com/s/1pJnAUr5 2.<Spark纯实战公益大讲坛>http://pan.baidu.com/s ...
- iOS:操作队列实现多线程NSOperation
NSOperation具体使用:直接继承NSObject 它的子类有:NSBlockOperation.NSInvocationOperation 还有一个必须的类,队列,用来装创建的线程 NSOpe ...
- Python学习之路上的几个经典问题
1.python有三元运算符语法(类似C语言的"?")么? 语法如下: [on_true] if [expression] else [on_false] 如果[expressio ...
- 怎样用Jenkins触发还有一个Jenkins---Global build solution
由于上次发的帖子太受欢迎,导致有非常多人问也有很多其它的人想知道.2个不同地域位置的Jenkins怎样自己主动触发相互的Job.当今非常多公司做的产品仅仅是全球化工作的一部分.须要这部分做好以后去做另 ...
- OpenCV 4.1 编译和配置
OpenCV 4.0 版本,历时3年半,终于在2018年圣诞节前发布了,该版本增加的新功能如下: 1) 更新代码支持 c++11 特性,需要兼容 c++11 语法的编译器 2)增加 dnn 中的模块功 ...
- Java中equals()、equalsIgnoreCase()和==的区别
用久了C#,在Java中,判断一个字符串还是习惯性的用了==,但是总是不能按照正确的判断分支运行,后来才想起来Java中是有equals的,然后就有引出了equalsIgnoreCase. 这三种 ...
- Win7如何重建桌面图标缓存
[已解决] windows7快捷方式图标丢失的解决方案(已解决) windows7快捷方式图标丢失的解决方案转自:http://iso1.com/2010/01/14/how-to-restore-w ...
- 利用POI进行Excel的导出
需求:将用户的违约金信息导出为excel表格格式 步骤 1. 数据库中增加按钮的值(注意上级编号要和页面隐藏域中的相等) DZ内容(页面加载时根据SJBH查询数据库内容,读取DZ字段信息并加载样式及方 ...