LeetCode OJ Add Two Numbers 链表求和
来源:互联网 发布:房地产端口可以退吗 编辑:程序博客网 时间:2024/06/05 16:22
Add Two Numbers
Total Accepted: 49110 Total Submissions: 219893You 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) { ListNode result(0); ListNode *p=&result; int t=0; int num1,num2,sum; while(l1 || l2) { num1=(l1?l1->val:0); num2=(l2?l2->val:0); sum=num1+num2+t; t=sum/10; sum=sum%10; ListNode *pNew=new ListNode(sum); p->next=pNew; p=pNew; if(l1) l1=l1->next; if(l2) l2=l2->next; } if(t>0) { ListNode *pNew=new ListNode(t); p->next=pNew; } return result.next; } };
0 0
- LeetCode OJ Add Two Numbers 链表求和
- [C++]LeetCode: 108 Add Two Numbers (反序链表求和)
- Leetcode 2. Add Two Numbers(链表求和)
- LeetCode OJ:Add Two Numbers
- LeetCode OJ - Add Two Numbers
- LeetCode OJ Add Two Numbers
- LeetCode OJ - Add two Numbers
- LeetCode OJ ----Add Two Numbers
- LeetCode OJ:Add Two Numbers
- Leetcode OJ:Add Two Numbers
- 链表求和问题(add two numbers)
- lintcode add-two-numbers 链表求和
- [LeetCode OJ][002]Add Two Numbers
- 【LeetCode OJ 002】Add Two Numbers
- LeetCode OJ #2 Add Two Numbers
- LeetCode OJ 02 Add Two Numbers
- LeetCode OJ : 2 Add Two Numbers
- <LeetCode OJ> 2. Add Two Numbers
- 黑马程序员————Objective C——内存管理
- 版本控制:github使用学习
- Socket与Http的区别
- Linux基础入门(详细教程)
- linux GDB调试core文件
- LeetCode OJ Add Two Numbers 链表求和
- How the iPhone knows where you are
- xml零配置之WebMvcConfigurationSupport
- pcm 相关参数
- java用org.apache.poi包操作excel
- 欢迎使用CSDN-markdown编辑器
- 第3周项目5多文件组织
- Number of 1 Bits
- 业界对生成图片缩略图的做法归纳