Add Two Numbers - C++链表操作
题目意思很简单,两个链表分别表示两个数,将两个数相加的结果存入一个新的链表中。
思路同样很简单:两个链表如果一样长,对应位置相加,如果某一个链表多了,则根据加的结果有无进位继续处理,全部结束后要考虑会不会还剩进位。
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++链表操作的更多相关文章
- LeetCode 2 Add Two Numbers(链表操作)
题目来源:https://leetcode.com/problems/add-two-numbers/ You are given two linked lists representing two ...
- LeetCode 2. add two numbers && 单链表
add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...
- 【LeetCode每天一题】Add Two Numbers(两链表相加)
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- LeetCode第[2]题(Java):Add Two Numbers (链表相加)——Medium
题目难度:Medium 题目: You are given two non-empty linked lists representing two non-negative integers. The ...
- LeetCode OJ:Add Two Numbers (相加链表之数)
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 445. Add Two Numbers II 链表中的数字求和
[抄题]: You are given two non-empty linked lists representing two non-negative integers. The most sign ...
- 链表求和12 · Add Two Numbers
反向存储,从左往右加 [抄题]: 你有两个用链表代表的整数,其中每个节点包含一个数字.数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头.写出一个函数将两个整数相加,用链表形式返回和.给 ...
- LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表
题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...
- 【leetcode】Add Two Numbers
题目描述: You are given two linked lists representing two non-negative numbers. The digits are stored in ...
随机推荐
- delphi 修改代码补全的快捷键(由Ctrl+Space 改为 Ctrl + alt + Space)
delphi 的IDE快捷键与输入法切换键中突,以往的解决方法是下载一个ImeTool修改 windows 系统的快捷键 在 xp win7 都好使,但在win 10经常是修改完后,重启又失效了. 本 ...
- mybatis.generator.configurationFile
mybatis.generator.configurationFile 有一个更好的配置方法,可以不用在generateConfig.xml里面写死驱动的地址:如果你的mybatis连接也是在pom. ...
- mysql查询语句理解
看一个查询语句 ,)) as passcount FROM (SELECT b.user,b.full_name,b.user_group From login_log a LEFT JOIN vic ...
- cdoj 邱老师看电影
//第一次写概率dp //写成记忆化搜索的形式比递推要更方便易懂 //不过好像还是可以写成递推的形式的 但是比较那个…… #include<cstdio> #include<iost ...
- 面向对象程序设计-C++_课时14对象组合_课时15继承
对象组合,就是一个类的对象作为另外一个类的成员,涉及类的对象,对象是实体,玩实 继承,涉及类,类是概念,玩虚 public: 所有人都可以接触 private: 数据放private protecte ...
- java 请求响应乱码
package org.operamasks.servlet; import java.io.IOException; import java.io.PrintWriter; import java. ...
- JavaScript引用类型之Array数组的栈方法与队列方法
一.栈方法 ECMAScript数组也提供了一种让数组的行为类似与其他数据结构的方法.具体的来说,数组可以变现的向栈一样,栈就是一种可以限制插入和删除向的数据结构.栈是一种LIFO(Last In F ...
- vlan trunk vtp端口聚合
第一步:端口聚合(两端都需要做相同的操作) 第二步:在服务器端配置为服务器模式 第四步:在服务器端添加vlan 第五步:在两端分别将不同的端口添加到不同的vlan
- NSURLConnect 的简单实用(iOS8淘汰)
Demo_1 NSRULConnection NSRULConnection 苹果公司在ios8已经抛弃了,但是我还是要讲一下,因为这和后面的NSSession有着密切的联系 下面开始使用步骤: 1. ...
- SQL Server中 sysobjects、syscolumns、systypes
1.sysobjects 系统对象表. 保存当前数据库的对象,如约束.默认值.日志.规则.存储过程等 在大多数情况下,对你最有用的两个列是Sysobjects.name和Sysobjects.x ...