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. 关于URLEnCode,URLDeCode,Base64,公钥私钥

    1.Base64非常适合http.mime协议,所以在一些类似webservice中可以用Base64. 用法如下:传出去之前先 Convert.ToBase64String(encryptedByt ...

  2. grub4dos

    default 1 timeout 5 gfxmenu (hd0,0)/grub/message configfile #####以上不在第一分区无法加载 title HITSZ_COMMAND co ...

  3. test spring in category

    test tile package com.journaldev.spring.controller; import java.text.DateFormat; import java.util.Da ...

  4. ios 设置声音和震动,单独控制

    一.今天项目中涉及了设置这快的声音震动和响铃,搞的头大,以前搞过,只是简单的调用系统的方法就可以实现,但是现在的公司要求,震动是震动,响铃是响铃,我看了微信,微信也是的分开的,做的很好,但是我就纳闷了 ...

  5. [转]在 ASP.NET MVC 4 中创建为移动设备优化的视图

    原文链接 https://msdn.microsoft.com/zh-cn/magazine/dn296507.aspx 如果深入探讨有关编写移动设备网站的常识性考虑因素,会发现其中有一种内在矛盾.  ...

  6. .NET Framework 的 Quirk Version

    今天在CSDN上看到一个帖子 :".net 4.0和4.5不同版本的Uri.ToString行为不同?", 调试.NET Framework 源代码发现,是这句代码起的作用 int ...

  7. Opencv配置问题_Error LNK2019

    终于配好opencv(Win7 64位+VS2013+opencv2.4.9),兴奋的写了第一个程序(当然是显示lena的玉照了): #include <opencv2\opencv.hpp&g ...

  8. 第六百一十八天 how can I 坚持

    此刻好烦,乱七八糟的,红米2死机也没弄好,哎. 下周三去长城,感觉还有很大提升空间啊,什么都不会.哎. 眼累的不行了,得抓紧睡觉了.ls他们来北京开年会了.. 明天,zjp来找我玩呢. 睡觉了.累.

  9. 通过浏览器https能够访问SVN,但eclipse SVN,tortoiseSVN始终连接不上SVN的问题解决方案

    为了便于本地代码维护,特意在本地搭建了一个visualSVN服务器用于本地代码管理,但是最近突然出现问题,eclipse上的SVN资源库始终连接不上,提示 "svn: connection ...

  10. 修改tomcat应用日志默认编码格式

    前言 今天开发跟我说tomcat日志中的中文不能正常显示,根据以往的经验,我觉得可能跟服务器的编码有关,于是尝试各种方法,但还是没能解决问题. 后来我突然想到会不会跟tomcat的设置有关呢,于是在网 ...