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. jQuery 判断所有图片加载完成

    对于图片的处理,例如幻灯片播放.缩放等,都是依赖于在所有图片完成之后再进行操作. 今天来看下如何判断所有的图片加载完成,而在加载完成之前可以使用 loading 的 gif 图表示正在加载中. 一.普 ...

  2. 【转】JavaScript里的this指针

    用自然语言的角度理解JavaScript中的this关键字 <script type="text/javascript"> function ftn03(){ var ...

  3. js设计模式(5)---外观模式

    0.前言 早上好,今天天气不错,估计有35度吧,坐在空调室里相当惬意,那么酒足饭饱之后就应该干些正事了. 1. 为什么使用外观模式 外观模式提供了一个高层接口,封装一些复杂操作或繁琐行为,方便调用.门 ...

  4. 杂项一之js,<select>标签

    一.在aspx页面中实现 修改与删除页面的跳转 前台js部分: 在上部的js部分中写,根据传过来的id,来经行页面的跳转,并把id传过去 js部分就是实现了一个页面跳转的功能 (还有确认框confir ...

  5. linux 关机方式

    linux 关机命令: 1-  init 0 关机. 具体详情接受可以 init --help 查询 如下: init [OPTIONS...] {COMMAND} Send control comm ...

  6. Jexus 高并发请求的优化技巧 笔记

    Jexus web server 5.1 每个工作进程的最大并发数固定为1万,最多可以同时开启4个工作进程,因此,每台Jexus V5.1服务器最多可以到支持4万个并发连接.但是,按照linux系统的 ...

  7. AngularJS(11)-API

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 基于jquery打造的网页右侧自动收缩浮动在线客服代码

    基于jquery打造的网页右侧自动收缩浮动在线QQ客服代码, 当前比较流行的一款QQ在线jquery特效代码, 代码中还带有IE6下PNG图片透明的特效,如果想研究IE6下PNG透明的同学也可以下载研 ...

  9. 跟着PHP100第一季学写一个CMS(11-20)

    PS.刚发现IE并不兼容,有点悲剧  cms1.1密码处理 1.小知识:sha1($_answer) //加密为40位,不知道能不能破解 2.小知识:mysql_real_escape_string( ...

  10. 为什么24位位图(真彩色)的biSizeImage不等于(biWidth*biBitCount+31)/32*4*biHeight?

    规定的,规定BMP文件的像素数据是按行存储的,而且每行的字节数必须为4的倍数,如果实际的像素数据不是4的倍数咋办?这就需要字节对齐,对齐是在一行的末尾添0以补足一行的字节数为4的倍数, ( biWid ...