题目意思很简单,两个链表分别表示两个数,将两个数相加的结果存入一个新的链表中。

思路同样很简单:两个链表如果一样长,对应位置相加,如果某一个链表多了,则根据加的结果有无进位继续处理,全部结束后要考虑会不会还剩进位。

c++的链表,题目已经给了一个挺好的例子:

struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};

对于创建链表可以使用一个头节点来一直指向这个链表,头节点中没有数据

ListNode *l1,*l1head;
l1=new ListNode();
l1head=l1; for(int i=;i<n;i++)
{
scanf("%d",&temp);
ListNode *tempnode=new ListNode(temp);
l1->next=tempnode;
l1=l1->next;
}

接下来直接进行比较,情况考虑周全即可,比较危险的数据有

[5],[5] ; [1],[99]

 class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *res, *head;
res = new ListNode();
head = res;
int flag = ;
while (l1 != NULL && l2 != NULL) {
ListNode *tempNode = new ListNode();
int temp = l1->val + l2->val + flag;
if (temp >= ) {
flag = ;
tempNode->val = temp % ;
} else {
flag = ;
tempNode->val = temp;
} l1 = l1->next;
l2 = l2->next;
res->next = tempNode;
res = res->next;
}
while (l1 != NULL) {
ListNode *tempNode = new ListNode(); if (flag == ) { tempNode->val = l1->val + flag;
if (tempNode->val >= ) {
flag = ;
tempNode->val = tempNode->val % ;
} else {
flag = ;
}
l1 = l1->next;
res->next = tempNode;
res = res->next;
} else {
res->next = l1;
break;
} }
while (l2 != NULL) {
ListNode *tempNode = new ListNode(); if (flag == ) { tempNode->val = l2->val + flag;
if (tempNode->val >= ) {
flag = ;
tempNode->val = tempNode->val % ;
} else {
flag = ;
}
l2 = l2->next;
res->next = tempNode;
res = res->next;
} else {
res->next = l2;
break;
} }
if (flag == ) {
ListNode *tempNode = new ListNode();
res->next = tempNode;
}
return head->next;
}
};

Add Two Numbers

PS :

调用和返回都使用了p->next的方式,因为没有把头指针传进去。

Add Two Numbers - C++链表操作的更多相关文章

  1. LeetCode 2 Add Two Numbers(链表操作)

    题目来源:https://leetcode.com/problems/add-two-numbers/ You are given two linked lists representing two ...

  2. LeetCode 2. add two numbers && 单链表

    add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...

  3. 【LeetCode每天一题】Add Two Numbers(两链表相加)

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  4. LeetCode第[2]题(Java):Add Two Numbers (链表相加)——Medium

    题目难度:Medium 题目: You are given two non-empty linked lists representing two non-negative integers. The ...

  5. LeetCode OJ:Add Two Numbers (相加链表之数)

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

  6. 445. Add Two Numbers II 链表中的数字求和

    [抄题]: You are given two non-empty linked lists representing two non-negative integers. The most sign ...

  7. 链表求和12 · Add Two Numbers

    反向存储,从左往右加 [抄题]: 你有两个用链表代表的整数,其中每个节点包含一个数字.数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头.写出一个函数将两个整数相加,用链表形式返回和.给 ...

  8. LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表

    题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...

  9. 【leetcode】Add Two Numbers

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

随机推荐

  1. 剑指offer26 复杂链表的复制

    /* struct RandomListNode { int label; struct RandomListNode *next, *random; RandomListNode(int x) : ...

  2. MyBatis使用Generator自动生成代码

    MyBatis中,可以使用Generator自动生成代码,包括DAO层. MODEL层 .MAPPING SQL映射文件. 第一步: 配置好自动生成代码所需的XML配置文件,例如(generator. ...

  3. SQLite for C#

    slqlite是个轻量级的数据库,是目前较为流行的小型数据库,适用于各个系统..NET自然也是支持的 1.添加2个引用System.Data.SQLite.Linq,System.Data.SQLit ...

  4. HTML系列(二):头部meta元素

    有关name: 一.页面关键字 网站关键字:用户通过搜索引擎能搜到该网站的词汇.最好控制在10个以内. 基本语法: <meta name="keywords" content ...

  5. 编码神器 Sublime Text 包管理工具及扩展大全

    Sublime Text 是程序员们公认的编码神奇,拥有漂亮的用户界面和强大的功能,例如代码缩略图,多重选择,快捷命令等.还可自定义键绑定,菜单和工具栏.Sublime Text 的主要功能包括:拼写 ...

  6. swipe.js文档及用法

    最近的一个项目中使用到了swipe.js这个插件 感觉非常的好用的 官方网站 http://swipejs.com/ https://github.com/bradbirdsall/Swipe 简介 ...

  7. Spring MVC详细示例实战教程【转】

    一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 ...

  8. Deep Learning(深度学习)学习笔记整理系列之(六)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...

  9. html5 meta标签

     <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-sca ...

  10. 【转】CoreData以及MagicalRecord (一)

    先粗略的了解下CoreData中的一些核心概念 1. CoreData 的核心概念 先上两幅关键的概念图 (1)NSManagedObjectModel 托管对象模型(MOM)是描述应用程序的数据模型 ...