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 ...
随机推荐
- poj2388---求奇数个数字的最中间的数
#include <stdio.h> #include <stdlib.h> int cmp(const void *a,const void *b) { return (*( ...
- iOS定位功能
1.实现定位功能需要导入系统库MapKit.framework 2.在iPhone手机上默认是禁止手机定位的,所以,要询问系统是否开启手机定位功能. 为了开启手机定位功能,还需在info.plist中 ...
- ZOJ 3794 Greedy Driver spfa
题意: 给定n个点,m条有向边,邮箱容量. 起点在1,终点在n,開始邮箱满油. 以下m行表示起点终点和这条边的耗油量(就是长度) 再以下给出一个数字m表示有P个加油站,能够免费加满油. 以下一行P个数 ...
- hdu1114Piggy-Bank(DP完全背包)
题意:在ACM可以做任何事情,必须准备和预算获得必要的财政支持.这次行动的主要收入来自不可逆绑定金钱(IBM).背后的想法很简单.每当一些ACM成员有任何小的钱,他把所有的硬币和成小猪银行抛出.你知道 ...
- EF 4.0 更新数据时候的一个错误及其处理
错误如图: 修改下方法后可以进行更新了.但是中间多了一步查询 /// <summary> /// 更新一个产品分类 /// </summary> /// <param n ...
- C++ DLL注册
今天项目中需要用C++实现对几个DLL文件的注册,查了许多资料,总结如下: #include <SHLWAPI.H> #pragma region DLL 注册与反注册 add by wh ...
- XX秘籍
第一课 一个男人在他妻子洗完澡后准备进浴室洗澡.这时,门铃响了. 妻子迅速用浴巾裹住自己冲到门口. 当她打开门时,邻居鲍勃站在那儿. 在她开口前,鲍勃说,“你如果把浴巾拿掉,我给你800美元.” 想了 ...
- 前端页面优化:javascript图片延迟加载
自己写了个简单的图片延迟加载小插件. 功能如下: 页面刷新,当前屏幕图片直接加载真实地址,body被卷去的部分和下方未显示的部分的img均加载通用图片如:loding.gif 随着屏幕上下滚动加载相应 ...
- Mac OS X下Maven的安装与配置
Mac OS X 安装Maven: 下载 Maven, 并解压到某个目录.例如/Users/robbie/apache-maven-3.3.3 打开Terminal,输入以下命令,设置Maven cl ...
- java——String的那边破事
经典的先看下面一段代码,请问最终创建几个对象,分别在哪里? String s0 = new String("luoliang.me"); String s1 = "luo ...