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 ...
随机推荐
- cas服务器源码阅读笔记,对标博客
对标源码阅读博客:http://www.cnblogs.com/jiuzhongguo/category/375405.html 在CAS中很多地方使用了策略模式,那么根据什么方式来确定使用哪种策略呢 ...
- Unity-EasyTouch插件之One Finger
这节课,我们主要讲下单个手指的测试.比如单击啊,双击啊,拖动,单手滑动等. 单击: public class TouchTest : MonoBehaviour { // Subscribe to e ...
- iOS 捕获程序崩溃日志
iOS开发中遇到程序崩溃是很正常的事情,如何在程序崩溃时捕获到异常信息并通知开发者? 下面就介绍如何在iOS中实现: 1. 在程序启动时加上一个异常捕获监听,用来处理程序崩溃时的回调动作 NSSetU ...
- [转]SSIS包的调用方式
本文转自:http://www.cnblogs.com/lijun4017/archive/2008/12/04/1347701.html 编写简单SSIS包光看MSDN应该就问题不大了,最近几天几个 ...
- window环境下备份与恢复(实际操作)
C:\Documents and Settings\xuzhengzhu>sqlplus /nolog SQL*Plus: Release 10.2.0.1.0 - Production on ...
- python 实现简单的感知机
最近在自学机器学习,记录下一些学习记录 如何用python实现一个简单的感知机 需要安装numpy库,即下面用到的np 简单的说就是 通过计算权重向量w和输入向量x的线性组合,判断该线性组合是否大于某 ...
- 快速实现一个生产者-消费者模型demo
package jesse.test1; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.Blo ...
- C# this.Hide()
C# this.Hide() 第一次用的时候是在_Load函数里: BookSystem bs = new BookSystem(); bs.ShowDialog(); ...
- JCheckBox使用示例
// 初始化 JCheckBox chk=new JCheckBox("XXX"); // 选择 chk.setSelected(true); // 判断选择状态 chk.isSe ...
- FastIV图像处理
新建一图像处理算法群,主要讨论图像处理与计算机视觉中的快速算法及其工程实现. 群号码:322687422