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;
}

直接反转链表

有两种办法:

  1. 遍历节点,依次把节点插到head之前,每次插入后需要维护好head指针。
  2. 用3个指针来实现,反转next之后,3指针移动一个节点。

不需要反转链表的方法

  1. 先遍历两个链表,计算两个链表的长度
  2. 根据链表长度,对应节点相加
  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的更多相关文章

  1. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  2. [leetcode]445. Add Two Numbers II 两数相加II

    You are given two non-empty linked lists representing two non-negative integers. The most significan ...

  3. LeetCode 445. Add Two Numbers II (两数相加 II)

    题目标签:Linked List 题目给了我们两个 数字的linked list,让我们把它们相加,返回一个新的linked list. 因为题目要求不能 reverse,可以把 两个list 的数字 ...

  4. LeetCode 445. Add Two Numbers II(链表求和)

    题意:两个非空链表求和,这两个链表所表示的数字没有前导零,要求不能修改原链表,如反转链表. 分析:用stack分别存两个链表的数字,然后从低位开始边求和边重新构造链表. Input: (7 -> ...

  5. 445. Add Two Numbers II - LeetCode

    Question 445. Add Two Numbers II Solution 题目大意:两个列表相加 思路:构造两个栈,两个列表的数依次入栈,再出栈的时候计算其和作为返回链表的一个节点 Java ...

  6. 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...

  7. 【Leetcode】445. Add Two Numbers II

    You are given two non-empty linked lists representing two non-negative integers. The most significan ...

  8. 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 ...

  9. *445. Add Two Numbers II

    1. 原始题目 You are given two non-empty linked lists representing two non-negative integers. The most si ...

随机推荐

  1. 数据结构与算法(1)支线任务3——Largest Rectangle in Histogram

    题目如下:(https://leetcode.com/problems/largest-rectangle-in-histogram/) Given n non-negative integers r ...

  2. linux pxe+dhcp+nfs+tftp

    yum -y install vsftpd dhcp xinetd tftp-server syslinux(安装"syslinux"才有pxelinux.0) tftp 服务(v ...

  3. nodejs + ionic2 + cordova环境搭建

    1.nodejs 下载地址:https://nodejs.org/zh-cn/,执行默认安装就可以,可执行命令 node(环境变量) 2.修改 npm 源,执行:npm config set regi ...

  4. Apache 日志管理,获取客户端端口号

    日志管理分类 日志文件是用户管理和监控 Apache 安全的非常好的第一手资料,它清晰地记录了客户端访问 Apache 服务器资源的每一条记录,以及在访问中出现的错误信息,可以这样说,Apache 可 ...

  5. onethink连接操作 sqlite 数据库

    直接上干货:一个简单的demo onthink本身已经有sqlite数据库的驱动 不需要在下载 common下面的config文件: 'SQLITE'=> array( 'DB_TYPE' =& ...

  6. c++ 打印堆栈代码

    namespace google {namespace glog_internal_namespace_ {void DumpStackTraceToString(std::string* stack ...

  7. Window 对象详解 转自 http://blog.csdn.net/jcx5083761/article/details/41243697

    详解HTML中的window对象和document对象 标签: HTMLwindowdocument 2014-11-18 11:03 5884人阅读 评论(0) 收藏 举报 分类: HTML& ...

  8. mysql 模糊查询语句比较(LIKE、instr、locate、find_in_set、position)

    大家都知道mysql 模糊查询的常用方法是LIKE 但这个语句查询效率很慢,那么有没有比较好的方法呢,下面本人测试了几个语句 测试数据800条左右 1,

  9. 数据库FMDB-sql

    1.首先要先导入第三方类库FMdatabase. 2.获得存放数据库文件的沙盒地址. #pragma mark - 创建数据库 - (void)createAndInitDatabase { NSSt ...

  10. 让window.close不提示:您查看的网页正在试图关闭窗口。是否关闭此窗口?

    正常来说,当我们调用window.close来关闭从地址栏中打开的窗口时,IE会弹出提示说:您查看的网页正在试图关闭窗口,是否关闭此窗口? 你可以将window.close替换成下边的脚本,然后再测试 ...