LeetCode--Add Two Numbers(链表)
来源:互联网 发布:阿里云服务器开发端口 编辑:程序博客网 时间:2024/05/16 19:49
双链表的加法。
代码:
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { vector<int> ans; int overFlow = 0; ListNode* tmpNode = NULL; while(l1 != NULL||l2 != NULL||overFlow != 0) { int tmpVal,val; if(l1 != NULL&&l2 != NULL) { tmpVal = l1->val+l2->val+overFlow; if(tmpVal >= 10) { val = tmpVal%10; overFlow = tmpVal/10; } else { val = tmpVal; overFlow = 0; } } else if(l1 == NULL&&l2 == NULL) { val = overFlow; overFlow = 0; } else if(l1 == NULL) { tmpVal = l2->val+overFlow; if(tmpVal >= 10) { val = tmpVal%10; overFlow = tmpVal/10; } else { val = tmpVal; overFlow = 0; } } else if(l2 == NULL) { tmpVal = l1->val+overFlow; if(tmpVal >= 10) { val = tmpVal%10; overFlow = tmpVal/10; } else { val = tmpVal; overFlow = 0; } } ans.push_back(val); if(l1) { l1 = l1->next; } if(l2) { l2 = l2->next; } } int nSize = ans.size(); for(int i = nSize-1;i >= 0;i--) { ListNode* newNode = new ListNode(ans[i]); newNode->next = tmpNode; tmpNode = newNode; } return tmpNode; }};
0 0
- LeetCode--Add Two Numbers(链表)
- leetcode Add Two Numbers 链表
- leetcode--Add Two Numbers--链表
- leetcode---Add Two Numbers---链表
- leetcode---add-two-numbers---链表
- LeetCode: Add Two Numbers
- LeetCode: Add Two Numbers
- [LeetCode]Add Two Numbers
- LeetCode Add Two Numbers
- Leetcode: Add Two Numbers
- LeetCode : Add Two Numbers
- LeetCode Add Two Numbers
- [LeetCode] Add Two Numbers
- Leetcode : Add Two Numbers
- 【leetcode】Add Two Numbers
- [LeetCode]Add Two Numbers
- leetcode Add Two Numbers
- LeetCode-Add Two Numbers
- 语音识别技术学习系列(一)——入门
- 填算式
- MATLAB的FFT变化总结
- 1~100所有整数出现9的次数
- CSS中的clip属性
- LeetCode--Add Two Numbers(链表)
- 代码实现获取log日志和logcat使用方法
- B - Dungeon Master POJ2251 三维的图 进行搜索,注意三维图的读入细节
- oc_study17
- 基础---[Android记录]自定义ADT模板
- 爱情:溺水三千只取一瓢饮
- 【Objective-C】关于objective-c中的类的总结
- ComponentName
- win7系统下右键无新建“快捷方式”解决办法