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

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

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. C语言入门(8)——形参与实参

    对于带参数的函数,我们需要在函数定义中指明参数的个数和每个参数的类型,定义参数就像定义变量一样,需要为每个参数指明类型,并起一个符合标识符命名规则的名字.例如: #include <stdio. ...

  2. struts漏洞修补过程之S2-016

    Struts漏洞修补过程之S2-016.邪恶的Struts再次现身,这一次是远程执行漏洞.官方建议立即升级到2.3.15.1.真希望这是最后一次漏洞修补.下面是升级步骤. 1.升级到struts2.3 ...

  3. 判断一个指定的Service是否存在的方法

    这是一个判断一个指定的Service是否存在的方法.它被用于监视一个Service是否由于已经运转,如果由于各种原因Service已经被停止了.这是在重新启动指定Service.它被用于一个Appli ...

  4. pwd显示链接文件的真实路径

    1.pwd用于显示当前工作路径 2.pwd -P 用于显示当前的实际工作路径(主要用于链接文件) 加参数难以了理解,看个例子就明白了: 进入链接文件,pwd显示的是链接文件所在的路径,而你不是链接文件 ...

  5. 自己写的操作sql的公共类

    /* /' `\/ `. . .' : `. `. \\.' , `.` `. `. ,___/|\. `. : . \, .'./ ' '\ , ' .\ . \_.~ _; ; \/'. `\ . ...

  6. Android入门——UI(7)——Fragment

    先上fragment静态加载的代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...

  7. Windows Server 中开启 SQL Server 2008 的1433端口

    在Windows Server2008 服务器上部署了Microsofit SQL Server2008 R2 ,想让远程机器能够访问,于是开放1433端口,进行了如下设置: 1.打开“本地安全策略” ...

  8. c++ 文件读写(转)

    C/C++ code //创建一个文本文件并写入信息 //同向屏幕上输出信息一样将信息输出至文件 #include<iomanip.h> #include<fstream.h> ...

  9. tomcat最大线程数的设置(转)

    1.Tomcat的server.xml中连接器设置如下 <Connector port="8080" maxThreads="150" minSpareT ...

  10. linux杂记(?)命令别名——alias

    linux中的命令别名--alias linux中的命令别名--alias 1. 用命令alias可以查询系统中有哪些可用的命令别名 2.添加命令别名 回车查看, 别名建立成功 3.查询命令的地址ty ...