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. Redis集群方案及实现

    在作出Redis群集解决方案,他跑了小半个.行表现得非常稳定在几乎相同的经历与大家分享,我写在前面的文章数据在线服务的一些探索经验,能够做为背景阅读 应用 我们的Redis集群主要承担了下面服务:1. ...

  2. spring 整合quartz的方式——简单介绍

    一.继承QuartzJobBean,重写executeInternal方法 <bean name="statQuartzJob" class="org.spring ...

  3. Maven直接部署Web应用Tomcat

    1. 下载解压版tomcat,并配置环境变量.所以tomcat你可以成功启动. 使用版本解压tomcat可以方便查看tomcat的后台输出的出错信息,便于调试. 2. 给tomcat配置用户名密码. ...

  4. Android深入研究Adapter重绘

    一直以来Adapter的使用都仅仅是流于表面,仅仅知道要实现几个抽象的方法,把Adapter设置给某种listView,就能够非常好的工作起来.所谓理解仅仅是建立在主观的猜想上面,认为应该是这样,对, ...

  5. Gradle增量学习建筑

    请在本系列下面的文章下载Github演示示例代码: git clone https://github.com/davenkin/gradle-learning.git       假设我们Gradle ...

  6. WinForm LED循环显示信息,使用定时器Threading.Timer

    原文:WinForm LED循环显示信息,使用定时器Threading.Timer 这里用一个示例来演示timer如何使用.示例:LED屏幕显示描述:这个示例其实很简单,LED屏幕上显示3个信息:  ...

  7. C语言优化实例:为了消除嵌套switch-case聪明的做法

    我们有可能会写出或者遇到类似这种代码: C/C++ switch (expr1) { case label11: switch (expr2) { case label21: // do someth ...

  8. debugging python with IDLE

    1. start IDLE "Python 2.5"→"IDLE(Python GUI)" 2. open your source file window Fr ...

  9. 查询出各个学科的前3名的同学信息的Sql

    查找各个学科的成绩前3名的学生信息Sql,有2种方法,一种是利用sql的row_number() over()函数,另一种是用子查询, 表设计如下 如果不考虑各个学科的成绩有并列的情况的话,有如下两种 ...

  10. JAVA类型修饰符(public,protected,private,friendly)

    JAVA类型修饰符(public,protected,private,friendly) public的类.类属变量及方法.包内及包外的不论什么类均能够訪问:protected的类.类属变量及方法,包 ...