LeetCode 445 Add Two Numbers II
445-Add Two Numbers II
You are given two linked lists representing two non-negative numbers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
Example:
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7
题解
比2-Add Two Numbers稍微麻烦一点。简单的想法是反转两个链表,就变成了2-Add Two Numbers的问题,转换有两种方法,一种式直接在原来的链表上转换,一种是利用栈来转换。
利用栈
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
if (l1 == NULL) return l2;
if (l2 == NULL) return l1;
stack<int> st1, st2;
ListNode *head = NULL;
ListNode *cur = NULL;
int sum = 0;
int carry = 0;
while (l1 != NULL) {
st1.push(l1->val);
l1 = l1->next;
}
while (l2 != NULL) {
st2.push(l2->val);
l2 = l2->next;
}
//用两个while逻辑上差了些,但是减少了判断次数
while (!st1.empty()) {
if (!st2.empty()) {
sum = st1.top() + st2.top() + carry;
carry = sum > 9 ? 1 : 0;
cur = head;
head = new ListNode(sum % 10);
head->next = cur;
st1.pop();
st2.pop();
} else {
sum = st1.top() + carry;
carry = sum > 9 ? 1 : 0;
cur = head;
head = new ListNode(sum % 10);
head->next = cur;
st1.pop();
}
}
while (!st2.empty()) {
sum = st2.top() + carry;
carry = sum > 9 ? 1 : 0;
cur = head;
head = new ListNode(sum % 10);
head->next = cur;
st2.pop();
}
if (carry) {
cur = head;
head = new ListNode(1);
head->next = cur;
}
return head;
}
直接反转链表
有两种办法:
- 遍历节点,依次把节点插到head之前,每次插入后需要维护好head指针。
- 用3个指针来实现,反转next之后,3指针移动一个节点。
不需要反转链表的方法
- 先遍历两个链表,计算两个链表的长度
- 根据链表长度,对应节点相加
- 利用两个指针实现进位。对于进位,当前位需要进位时,高1位如果不是就9,直接进1位就结束,如果是9,需要进位到依次的高1位不是9才停止,也就是只要知道需要进位的位前的第一个不为9的位,就知道了进位的范围了。- piont1从头开始遍历
- 如果point1->val< 9令piont2 = point1
- 如果point1>9,对point2到point1的节点加1,模10
如果head大于10需要先生成一个节点。point1->val< 9的判断成功的次数有点多啊,不知道有没有优化的方法。
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
if (l1 == NULL) return l2;
if (l2 == NULL) return l1;
int len1 = 0;
int len2 = 0;
for (ListNode *p = l1; p != NULL; p = p->next) ++len1;
for (ListNode *p = l2; p != NULL; p = p->next) ++len2;
if (len1 < len2) {
int tmp = len1;
len1 = len2;
len2 = tmp;
ListNode *p = l1;
l1 = l2;
l2 = p;
}
ListNode *head = new ListNode(0);
ListNode *cur = head;
for (int i = len1 - len2; i != 0; --i) {
cur->next = new ListNode(l1->val);
cur = cur->next;
l1 = l1->next;
}
while (l1) {
cur->next = new ListNode(l1->val + l2->val);
cur = cur->next;
l1 = l1->next;
l2 = l2->next;
}
//有可能最高进位,我习惯head是空,为了方便,如果进位就直接进到head
//返回时通过head->val是0或1确定返回head还是head->next
ListNode *bound = head;
cur = head->next;
while (cur) {
if (cur->val < 9)
bound = cur;
else if (cur->val > 9) {
while (bound != cur) {
bound->val = (bound->val + 1) % 10;
bound = bound->next;
}
cur->val -= 10;
}
cur = cur->next;
}
if (head->val == 1)
return head;
else
return head->next;
}
LeetCode 445 Add Two Numbers II的更多相关文章
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [leetcode]445. Add Two Numbers II 两数相加II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
- LeetCode 445. Add Two Numbers II (两数相加 II)
题目标签:Linked List 题目给了我们两个 数字的linked list,让我们把它们相加,返回一个新的linked list. 因为题目要求不能 reverse,可以把 两个list 的数字 ...
- LeetCode 445. Add Two Numbers II(链表求和)
题意:两个非空链表求和,这两个链表所表示的数字没有前导零,要求不能修改原链表,如反转链表. 分析:用stack分别存两个链表的数字,然后从低位开始边求和边重新构造链表. Input: (7 -> ...
- 445. Add Two Numbers II - LeetCode
Question 445. Add Two Numbers II Solution 题目大意:两个列表相加 思路:构造两个栈,两个列表的数依次入栈,再出栈的时候计算其和作为返回链表的一个节点 Java ...
- 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...
- 【Leetcode】445. Add Two Numbers II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
- 445. Add Two Numbers II ——while s1 or s2 or carry 题目再简单也要些测试用例
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- *445. Add Two Numbers II
1. 原始题目 You are given two non-empty linked lists representing two non-negative integers. The most si ...
随机推荐
- Mac 配置Charles抓https的包
安装Charles 这个简单,略过... 打开Charles,在Menu选择Help > Install Charles CA SSL Certificate Keychain Access(钥 ...
- Study plan for automation test framework
虽然部门的automation建立起来有两年多,去年项目一直很忙,仅限于应用(e.g 运行脚本测试或者写一些简短的测试脚本),但是一直没有深入研究其组成框架.近期希望抽出时间来做深入学习. 初步计划从 ...
- C#编写WIN32系统托盘程序
基本功能概述: 程序运行后驻留系统托盘,左键呼出,右键退出.后续可加右键菜单. 注册系统案件WIN+F10,呼出程序. 重写系统消息,最小化和关闭按钮隐藏程序 using System; using ...
- Linux系统安全保护措施
1.系统安全记录文件 操作系统内部的记录文件是检测是否有网络入侵的重要线索.如果系统是直接连接到Internet的,且发现有很多人对系统做telnet/FTP登录尝试,可以运行“#more/var/s ...
- Linux网络基本配置
一.Linux网络配置文件 1. /etc/sysconfig/network-scripts/ifcfg-eth0 文件 在Red Hat系统中,系统网络设备的配置文件保存在/etc/syscon ...
- 在MFC中使用GDI+的一般方法,以VC6.0编译器为例
1.载解压GDI+开发包: 2.正确设置include & lib 目录: 设置如下:VC6.0编译器菜单Tools->Options->Directories中添加inlude ...
- bonext.js学习笔记
bonext.js是个什么鬼? 首先这是一个前端开发框架,建立在Backbone.js的基础上,使用Jquery操作Dom,Bootstrap负责布局,Art-Template渲染模板,再加上自定义一 ...
- Opencv配置问题_Error LNK2019
终于配好opencv(Win7 64位+VS2013+opencv2.4.9),兴奋的写了第一个程序(当然是显示lena的玉照了): #include <opencv2\opencv.hpp&g ...
- PAT - 测试 01-复杂度2 Maximum Subsequence Sum (25分)
1, N2N_2N2, ..., NKN_KNK }. A continuous subsequence is defined to be { NiN_iNi, Ni+1N_{i ...
- CXF发布webService服务以及客户端调用
这篇随笔内容是CXF发布webService服务以及客户端调用的方法 CXF是什么? 开发工作之前需要下载CXF和安装 下载地址:http://cxf.apache.org 安装过程: <1&g ...