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. delphi cmd

    今天看到有人在问用代码执行CMD命令的问题,就总结一下用法,也算做个备忘. Delphi中,执行命令或者运行一个程序有2个函数,一个是winexec,一个是shellexecute.这两个大家应该都见 ...

  2. Libevent Not Found Error While Install Tmux

    First install libevent using –prefix=$HOME erro:“libevent not found” solve with using this when inst ...

  3. cordova-screenshot

    The Screenshot plugin allows your application to take screenshots of the current screen and save the ...

  4. Norflash控制器的Verilog建模之三(測試)

    前言:回校了,辦好手續就著手寫測試篇.初步的norflash控制器已經完成,通過硬件測試.目前的norflash完成扇区块擦除.单字节写.单字节读3个功能.博文最后附上源码. 总结:和之前的博文一样, ...

  5. 剑指Offer:解决难题时的三大方法

    1.画图 让抽象的东西变得直观生动起来.比如设计二叉树,链表,栈,队列这些数据结构时. 2.举例子 同样可以化抽象为直观.能够更清晰的展现思路.从例子归纳出一般做法. 3.分解 有时问题本身是比较复杂 ...

  6. Android自学笔记:Git下载源代码

    Info:做J2ME几年了,现在基本没有公司用了,是时候向Android领域进军了. 自学中,难免会有疏漏,有问题请及时提出,共同学习共同进步. 2014-10-13:初版 2014-10-14:添加 ...

  7. oracle 实现ID自增

    CREATE TABLE testTable1 ( ID INT NOT NULL, NAME ) NOT NULL, PRIMARY KEY(ID) ) TABLESPACE MYDB; --创建自 ...

  8. MS - 1 - 把二元查找树转变成排序的双向链表

    ## 1. 把二元查找树转变成排序的双向链表 ## ### 题目: 输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表. ### 要求不能创建任何新的结点,只调整指针的指向. 10       ...

  9. Opencv配置问题_Error LNK2019

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

  10. Linux下随机密码生成器

    参考资料: 1:http://justwinit.cn/post/5164/ 2:http://www.linuxidc.com/Linux/2012-11/73687.htm