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

思路:

看起来像是一道很简单的题,但是我却通过这道题发现了自己对指针掌握的不足。因为要返回一个链表,在链表的循环创建中指针是不断向下指的(ans),所以肯定要保留一个头结点(anshead)。就是在链表和链表头结点上,我纠结了很久。

首先,定义  anshead = new ListNode(0);

ans = anshead;

必须先给anshead分配内存,再把ans指向anshead分配的空间中,每次也必须先给 ans->next = new ListNode(0) 赋值后才能写 ans = ans->next;

总之,必须把ans 指向一个开辟过的空间。

否则, pre->next 在未开辟时指向 0x00000000 若这时 ans = pre->next 那么ans也是0x00000000 若这时给 ans 分配了空间,pre->next 还是0x00000000 因为之前ans没有明确的指定一个地方。

题目中只要注意进位就可以了。

class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
if(l1 == NULL) return l2;
if(l2 == NULL) return l1; ListNode * anshead = new ListNode();
ListNode * ans = anshead;
int cur = ; //当前位的个位数
int up = ; //进位 while(ans != NULL) //当没有下一位可以产生时就结束循环
{
cur = up;
if(l1 != NULL)
{
cur += l1->val;
l1 = l1->next;
}
if(l2 != NULL)
{
cur += l2->val;
l2 = l2->next;
}
up = cur / ;
cur = cur % ;
ans->val = cur;
if(!(up == && l1 == NULL && l2 == NULL)) //有进位 或 l1 l2 不空时才会有下一位
{
ans->next = new ListNode();
}
ans = ans->next;
} return anshead;
} void createList(ListNode * &L)
{
int n;
cin >> n;
if(n != -)
{
L = new ListNode(n);
createList(L->next);
}
}
};

看下大神同样思路,精简版的代码。

class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
ListNode preHead(), *p = &preHead;
int extra = ;
while (l1 || l2 || extra) {
int sum = (l1 ? l1->val : ) + (l2 ? l2->val : ) + extra;
extra = sum / ;
p->next = new ListNode(sum % );
p = p->next;
l1 = l1 ? l1->next : l1;
l2 = l2 ? l2->next : l2;
}
return preHead.next; //不要第一个自己随意赋的值,好方法
}
};

【leetcode】Add Two Numbers(middle) ☆的更多相关文章

  1. 【leetcode】Add Two Numbers

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

  2. 【题解】【链表】【Leetcode】Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  3. 【leetcode】 Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  4. 【LeetCode】Add Two Numbers(两数相加)

    这道题是LeetCode里的第2道题. 题目要求: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将 ...

  5. 【LeetCode445】 Add Two Numbers II★★

    题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...

  6. 【LeetCode】386. Lexicographical Numbers 解题报告(Python)

    [LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  7. 【Leetcode】【Medium】Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  8. 【leetcode】Compare Version Numbers(middle)

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

  9. 【链表】Add Two Numbers

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

随机推荐

  1. 如何解决win7系统访问共享服务器慢的问题

    最近重新装了win7系统,但发现当访问共享服务器的时候很慢,后来在网上搜索了相关解决办法,在此和大家分享下: 1. 通过“windows键”+“R键”快捷打开“运行”窗口,然后输入cmd命令敲回车,进 ...

  2. 使用JavaScript实现弹出层效果

    声明 阅读本文需要有一定的HTML.CSS和JavaScript基础 设计 实现弹出层效果的思路非常简单:将待显示的内容先隐藏,在触发某种条件后(如点击按钮),将原本隐藏的内容显示出来. 实现 < ...

  3. UE4 将本地图片转成UTexture2D 在runtime显示

    UFUNCTION(BlueprintCallable, Category = "TextureFromDisk") static class UTexture2D* GetTex ...

  4. jQuery WIN 7透明弹出层效果

    jQuery WIN 7透明弹出层效果,点击可以弹出一个透明层的jquery特效,插件可以调弹出框的宽度和高度,很不错的一个弹出层插件. 适用浏览器:IE8.360.FireFox.Chrome.Sa ...

  5. How to disable Passwords must meet complexity requirements[windows 7]

    The Password complexity is a Local Policy setting named "Passwords must meet complexity require ...

  6. Repeat Header / Keep Header Visible in Tables in RS 2008

    You selected "Repeat header rows on each page" or "Keep header rows visible while scr ...

  7. mysql中log

    mysql的主从模式配置 1.改主库配置文件:D:\Program Files\MySQL\MySQL Server 5.5(my.ini/my.cnf)在下面加入 [mysqld] log=c:/a ...

  8. EventHandler委托的使用

    今天复习了一下事件和委托,本来看事件来着,看到EventHandler,写了一个小例子,想贴在这里解释一下.为了弄清楚EventHandler, 还是回归到最基本的委托,曾经在园子里看到一位前辈用深入 ...

  9. 第一个C#应用 【搜索软件】

    搜索软件V1.0 [附软件截图][http://pan.baidu.com/s/1mihEbe4] 设备搜索:支持广播搜索[local search],指定ip[range search]搜索,直接w ...

  10. 转载---linux运维相关

    前段时间,我在准备面试的时搜到的一套Linux运维工程师面试题,感觉比较全面,一直保存在草稿,刚在整理后台时翻了出来,干脆就发出来好了,以备不时之需. 1.linux如何挂在windows下的共享目录 ...