【leetcode】Add Two Numbers(middle) ☆
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) ☆的更多相关文章
- 【leetcode】Add Two Numbers
题目描述: You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- 【题解】【链表】【Leetcode】Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【leetcode】 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【LeetCode】Add Two Numbers(两数相加)
这道题是LeetCode里的第2道题. 题目要求: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将 ...
- 【LeetCode445】 Add Two Numbers II★★
题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...
- 【LeetCode】386. Lexicographical Numbers 解题报告(Python)
[LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【Leetcode】【Medium】Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【leetcode】Compare Version Numbers(middle)
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- 【链表】Add Two Numbers
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
随机推荐
- AutoEventWireup="false"
在 Web 服务器控件中,某些事件(通常是 Click 事件)会导致窗体被回发到服务器.HTML 服务器控件和 Web 服务器控件(如 TextBox 控件)中的更改事件将被捕获,但不会立即导致发送. ...
- Linux 内核同步机制
本文将就自己对内核同步机制的一些简要理解,做出一份自己的总结文档. Linux内部,为了提供对共享资源的互斥访问,提供了一系列的方法,下面简要的一一介绍. Technorati 标签: ...
- 第二章 开始学习C++
第二章 开始学习C++ 2.1 main函数 简单代码如下: #include <iostream> int main() { //This is my first program u ...
- insertorupdate
MERGE INTO 运用的心得 最近完成一个功能,就是往表里插入数据,以party_id 和prod_id为联合主键,存在的更新,不存在的插入, ORACLE 10g 后可以试用MERGE INT ...
- 【风马一族_Android】手机与电脑通过adb进行连接
1:打开电脑的命令行 cmd 2:adb devices 查看与电脑连接的手机或模拟器的名称 3:准备要安装的apk.记住手机的名称 4:adb –s <模拟器名称> install & ...
- java中对集合对象list的几种循环访问
java中对集合对象list的几种循环访问的总结如下 1 经典的for循环 public static void main(String[] args) { List<String> li ...
- dedecms 处理分页样式及去掉分页li
最近装了个织梦dedecmsV5.7版本时,调用分页显示出现的结果出现好几行,怎么也不能在一排显示,找了很多资料,才了解到是由织梦模板中分页加了<Li>列表标签,解决有两种方法,下面将一一 ...
- Eclipse使用ButterKnife前,需要的配置步骤
ButterKnife下载地址(7.0.1版本):http://files.cnblogs.com/files/zzw1994/butterknife-7.0.1.zip 官方下载地址(7.0.1版本 ...
- 加载页面遮挡耗时操作任务页面--第三方开源--AndroidProgressLayout
在Android的开发中,往往有这种需求,比如一个耗时的操作,联网获取网络图片.内容,数据库耗时读写等等,在此耗时操作过程中,开发者也许不希望用户再进行其他操作(其他操作可能会引起逻辑混乱),而此时需 ...
- 查找指定表的字段not null约束,并生成删除Sql
SElECT 'ALTER TABLE '+OBJECT_NAME(c.parent_obj)+' DROP CONSTRAINT '+ c.name FROM sys.sysconstraints ...