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 ...
随机推荐
- Quartz_TimeJob例子(C#)
执行入口: using System; using System.Collections.Generic; using log4net; using Quartz; using ypt_base.Co ...
- VUE -- 如何快速的写出一个Vue的icon组件?
伴随着Vue的诞生,它似乎就被人寄予厚望,不仅仅是因为其轻量级的MVVM设计方式,而且其实现了组件化开发模式,所以越来越多的人会拿Vue和AngularJS.React Native做比较.具体关于它 ...
- static_cast ,reinterpret_cast
用法:static_cast < type-id > ( expression ) 该运算符把expression转换为type-id类型,但没有运行时类型检查来保证转换的安全性.它主要有 ...
- [转]SQL Server 创建数据库邮件
本文转自:http://www.cnblogs.com/gaizai/p/3358958.html 一. 背景 数据库发邮件通知数据库的运行状态(状态可以通过JOB形式获取)和信息,达到预警的效果. ...
- [转]SQLServer和Oracle,存储过程区别,常用函数对比
本文转自:http://www.cnblogs.com/neru/archive/2011/08/18/2144049.html 以前一直用sqlserver,只有很少的一点oracle的经验,现在要 ...
- Python学习1:使用Aptana构建Python开发环境
使用Aptana构建Python开发环境 下载Aptana: http://www.aptana.com/products/studio3/download http://www.newasp.net ...
- 《jQuery基础》总结
目前,互联网上最好的jQuery入门教材,是Rebecca Murphey写的<jQuery基础>(jQuery Fundamentals).这本书虽然是入门教材,但也足足有100多页.我 ...
- 784 - Maze Exploration
#include <stdio.h> #include <string.h> char maze[50][100]; void search(int i,int j) { if ...
- RDD原理与详解
RDD详解 原文连接 http://xiguada.org/spark_rdd/ RDD(Resilient Distributed Datasets弹性分布式数据集),是spark中最重要的概念,可 ...
- [转]php中实现事件驱动
原文: https://blog.csdn.net/yhl27/article/details/8705313 -------------------------------------------- ...