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. ZOJ 3623 Battle Ships 简单DP

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3623 题意:给出N种可以建造的船和对方的塔生命值L,每种船给出建造时 ...

  2. 024找到二维阵列(keep it up)

    剑指offer在标题中:http://ac.jobdu.com/problem.php? pid=1384 题目描写叙述: 在一个二维数组中,每一行都依照从左到右递增的顺序排序.每一列都依照从上到下递 ...

  3. 十依据一个有用的算法来找到最小(最大)的k的数量-线性搜索算法

    例如:进入1.2.3,4,5,6.7.8此8数字,最小的4图的1,2,3,4. 思路1:最easy想到的方法:先对这个序列从小到大排序.然后输出前面的最小的k个数就可以.假设选择高速排序法来进行排序, ...

  4. Cytoscape画图初探

    Cytoscape是一个做网络图的js插件.用起来非常方便,并且非常强大.这是它的站点:点击打开链接 使用它须要导入两个文件,一个是js文件,一个是css文件.官网上下载. 这里实现了一个功能.即从后 ...

  5. Matlab中调用第三方Java代码

    搞了一天,才算搞定. 第一步:定位Matlab中Java环境的ext目录 新建一个M script文件,或者直接在Matlab的交互式命令行中输入: disp(java.lang.System.get ...

  6. B/S 类项目改善

    B/S 类项目改善的一些建议   要分享的议题 性能提升:在访问量逐渐增大的同时,如何增大单台服务器的 PV2 上限,增加 TPS3 ? RESTful:相较于传统的 SOAP1,RESTful 风格 ...

  7. 纯CSS3实现的图片滑块程序 效果非常酷

    原文:纯CSS3实现的图片滑块程序 效果非常酷 之前我们经常会看到很多利用jQuery实现的焦点图插件,种类太多了,今天我想给大家分享一款利用纯CSS3实现的图片滑块应用,完全是利用CSS3的相关特性 ...

  8. printf交替使用

    今天附带printf一些替代实现. 转载请注明出处:http://blog.csdn.net/u010484477谢谢^_^ 我们总是用printf做各种输出语句: printf("%d&q ...

  9. 通过HttpClient来调用Web Api接口,实体参数的传递

    下面定义一个复杂类型对象 public class User_Info { public int Id { get; set; } public string Name { get; set; } p ...

  10. 大数据系列修炼-Scala课程10

    今天主要是关于Scala中对List的相关操作,list在Scala中应该是至关重要,接下来会讲解关于List的一系列操作 List的map.flatMap.foreach.filter操作讲解 1. ...