Problem:

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

一开始就是想到先把第一个list转换成一个数,然后把第二个转换成第二个数,然后相加后,再把相加的值变成list。代码如下

ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
int a = , b = , ans = , flag = ;
int index1 = , index2 = ;
ListNode *temp1, *temp2, *result;
temp1 = l1;
temp2 = l2;
while(temp1->next != NULL )
{
index1++;
a += (temp1 -> next -> val) * (int)pow(,index1);
}
while(temp2 -> next != NULL)
{
index2++;
b += temp2 ->next -> val * (int)pow(, index2);
}
ans = a + b;
result -> val = ans%;
result -> next = NULL;
flag = ans/;
while(flag)
{
ListNode *added = new ListNode(flag%10);
result ->next = added;
flag = flag/;
}
return result;
}

然后是Time Limit Exceed了。那就不能这样做,应该直接在链表相加。加到某个链表结束为止。要用中间变量记住当前的进位。如果最后进位不为零(也就是为1)的话,那还是需要记录的。代码贴出如下:

class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
{
ListNode * ans = NULL, *last = NULL;
int up = ;
while (l1 != NULL && l2 != NULL)
{
int tmp = l1->val + l2->val + up;
up = tmp / ;
if (last == NULL)
{
ans = new ListNode(tmp % );
last = ans;
}
else
last = pushBack(last, tmp % );
l1 = l1->next;
l2 = l2->next;
}
while (l1 != NULL)
{
int tmp = l1->val + up;
last = pushBack(last, tmp % );
up = tmp / ;
l1 = l1->next;
}
while (l2 != NULL)
{
int tmp = l2->val + up;
last = pushBack(last, tmp % );
up = tmp / ;
l2 = l2->next;
}
if (up == )
{
ListNode * l = new ListNode(up);
last->next = l;
}
return ans;
} ListNode * pushBack(ListNode * last, int val)
{
ListNode * l = new ListNode(val);
last->next = l;
return l;
}
};

还是要感谢suool大神,改天一定要再做一次看看是不是真的掌握了。自己真的水平有限啊。不过只要肯努力,一天进步一点点就好。让cnblogs记录我的学习过程,come on!

leetcode第四题--Add Two Numbers的更多相关文章

  1. Leetcode 第 2 题(Add Two Numbers)

    Leetcode 第 2 题(Add Two Numbers) 题目例如以下: Question You are given two linked lists representing two non ...

  2. leetcode 第二题Add Two Numbers java

    链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...

  3. LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表

    题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...

  4. LeetCode解题笔记 - 2. Add Two Numbers

    2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...

  5. LeetCode第四题,Add Two Numbers

    题目原文: You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  6. LeetCode第二题—— Add Two Numbers(模拟两数相加)

    Description: You are given two non-empty linked lists representing two non-negative integers. The di ...

  7. [算法题] Add Two Numbers

    题目内容 题目来源:LeetCode You are given two non-empty linked lists representing two non-negative integers. ...

  8. LeetCode(2)Add Two Numbers

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  9. LeetCode之“链表”:Add Two Numbers

    题目链接 题目要求: You are given two linked lists representing two non-negative numbers. The digits are stor ...

随机推荐

  1. don't touch your phone in any unfamiliar way(转)

    直到这两天 Chainfire 说,他正尝试在不修改手机 /System 分区的情况下获取 Android 系统 Root 权限,我才想起他在今年 9 月轻描淡写地提到自己已经把 SuperSU 卖给 ...

  2. ECLIPSE IDEA 调音 1

    为自己所用IDE进行jvm优 首先进行日志输出配置 Eclipse  改动eclipse.ini IDEA   改动 idea.exe.vmoptions 添加打印日志的配置參数 -XX:+Print ...

  3. Caused by: Unable to locate parent package [json-package] for [class com.you.action.ColumnAction] -

    1.错误叙述性说明 三月 15, 2015 7:53:25 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger error 严重: Dispat ...

  4. RSA JS 加密解密DEMO

    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script&g ...

  5. git 如何让单个文件回退到指定的版本

    1.进入到文件所在文件目录,或者能找到文件的路径查看文件的修改记录 1 $ git log MainActivity.java 结果: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  6. 快速入门:十分钟学会Python(转)

    初试牛刀 假设你希望学习Python这门语言,却苦于找不到一个简短而全面的入门教程.那么本教程将花费十分钟的时间带你走入Python的大门.本文的内容介于教程(Toturial)和速查手册(Cheat ...

  7. jsp 说明标签

    page指令 Page指令用来定义整个JSP页面的一些属性和这些属性的值. 比如我们能够用page指令定义JSP页面的contentType属性的值是text/html;charset=GB2312, ...

  8. [Shell]输入參数

    获取shell脚本的输入參数,而且推断得到的參数. #!/bin/bash #title: testPT.sh #atuhor: orangleliu #date: 2014-08-08 #desc: ...

  9. 经典算法题每日演练——第十六题 Kruskal算法

    原文:经典算法题每日演练--第十六题 Kruskal算法 这篇我们看看第二种生成树的Kruskal算法,这个算法的魅力在于我们可以打一下算法和数据结构的组合拳,很有意思的. 一:思想 若存在M={0, ...

  10. 变焦控制ZoomControls

    在安卓的webview中有这个点击放大缩小button,当时就在想如何实现那种效果,弄一个两个图标的ImageButton.但感觉又有些麻烦,昨天看疯狂安卓书.无意中发现另一个 ZoomButtons ...