LeeCode---Add Two Numbers
来源:互联网 发布:哪些网游国服有mac版 编辑:程序博客网 时间:2024/05/17 23:46
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
/** * 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) { int In=0; ListNode *pHead=new ListNode(0); ListNode *p=pHead; while(l1!=NULL||l2!=NULL) { int temp1=0; if(l1!=NULL) { temp1=l1->val; l1=l1->next; } int temp2=0; if(l2!=NULL) { temp2=l2->val; l2=l2->next; } int temp=temp1+temp2+In; p->next=new ListNode(temp%10); In=temp/10; p=p->next; } if(In==1) p->next=new ListNode(1); return pHead->next; } };
1 0
- LeeCode---Add Two Numbers
- [LeeCode]Add Two Numbers
- 2 Add Two Numbers LeeCode
- leecode algo2: Add Two Numbers (Java)
- LeeCode And Two Numbers
- Add two numbers & Add binary
- LeetCode: Add Two Numbers
- Add Two Numbers
- LeetCode: Add Two Numbers
- [LeetCode]Add Two Numbers
- LeetCode Add Two Numbers
- Leetcode: Add Two Numbers
- Add Two Numbers
- LeetCode : Add Two Numbers
- LeetCode Add Two Numbers
- [LeetCode] Add Two Numbers
- LeetCode4: Add Two Numbers
- Leetcode : Add Two Numbers
- HDU 5099 Comparison of Android versions(模拟-水题)
- Java编程:向枚举添加自定义属性(用for-each循环来打印每种苹果的名称和价格)
- 分享一张自己觉得很不错的Android学习线路图
- 菜鸟nginx源码剖析数据结构篇(十) 自旋锁ngx_spinlock
- 关于面向对象的的设计原则的使用
- LeeCode---Add Two Numbers
- C++ Primer再读笔记-1
- Cocos2d-x优化中多线程并发访问
- HDU 5095 Linearization of the kernel functions in SVM(模拟, 水题)
- js 遍历 object 属性和值的方法
- 第11周 项目6-5 输出1000以内的所有回文数
- 数据结构与算法学习之路:二分查找的非递归和递归算法
- HDU 5090 Game with Pearls(贪心, 二部图最大匹配)
- Servlet JSP 二重修炼:Filter过滤器