Leetcode日记(2)---Add two numbers
2015-12-01 17:42
357 查看
题目详解
两数相加,进位的处理,譬如7+8之后,获取和的个位数字和十位数字
问题追踪
查找了null的头文件,查询了cplusplus,头文件为stddef.h
#include<stddef.h> using namespace std; struct ListNode{ int val; ListNode *next; ListNode(int x):val(x), next(NULL){} }; class Solution{ public: ListNode *addTwoNumbers(ListNode *l1 , ListNode *l2){ int carry = 0; ListNode* tail = new ListNode(0); // kai shi lian biao shi shu zi wei 0 de lian biao ListNode* ptr = tail; while(l1 != NULL || l2 != NULL){ int val1=0; if(l1 != NULL){ val1 = l1->val; l1 = l1 ->next; } int val2 = 0; if(l2 != NULL){ val2 = l2->val; l2 = l2 ->next; } int tmp = val1 + val2 +carry; ptr->next = new ListNode(tmp % 10) ; // zhi liu xia ge wei shu zi carry = tmp /10 ; ptr = ptr ->next; } if (carry == 1){ ptr->next = new ListNode(1); } return tail->next; } }; int main(){ return 0; }
相关文章推荐
- Imageloader 配合image中的属性 显示缩略图
- 提高css渲染效率
- 15天玩转redis —— 第十篇 对快照模式的深入分析
- 算法—9.二叉查找树
- 80端口的烦恼:[3]清除NT Kernel占用80端口
- hdu 1241 田字格八个方向的深度优先搜索 求连通分量个数
- Python基础教程(六):list、tuple、dictionary
- Android中的Looper,Handler,Message和MessageQueue
- Centos 7安装Nginx 1.12.2
- 毕业生自传---(徘徊)
- Android平台的微信分享
- textfield修改placeholder的字体颜色和大小
- 如何查看80端口被占用
- Activiti学习笔记7 — ReceiveTask机器自动任务的使用
- log4net 学习
- IOS框架图(一)
- Python基础教程(五):数字、字符串
- iOS Touch ID
- maven常见问题解决
- 用户登陆的业务流程架构设计