[leetcode] add two numbers(大数据加法链表版)
2015-09-09 15:27
627 查看
Add Two Numbers
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) { ListNode *ans = new ListNode(0), *p = ans; int cur = 0; while(l1 != NULL || l2 != NULL || cur) { p->val = (l1 ? l1->val : 0) + (l2 ? l2->val : 0) + cur; cur = p->val / 10; p->val %= 10; if(l1) l1 = l1->next; if(l2) l2 = l2->next; if(l1 || l2 || cur) p->next = new ListNode(0); p = p->next; } return ans; } };
相关文章推荐
- 大数据单位 MB、GB、TB、PB、EB
- flume-hdfs 按照时间关闭并新开文件
- RAID0_RAID1_RAID10_RAID5各需几块盘才可组建
- myql_链接丢失异常_mybaits _等框架_报错_The last packet successfully
- ORA-01994: GRANT failed: password file missing or disabled
- 云计算:大数据时代的系统工程
- Kafka设计解析(三):Kafka High Availability (下)
- [刷题]Trailing Zeros
- Rails中用CSV导出中文真心有技巧
- Kafka设计解析(二):Kafka High Availability (上)
- configure: error: --with-ssl=openssl was given, but SSL is not available.
- [转载]Error -27796: Failed to connect to server
- 关于同步,异步,阻塞,非阻塞,IOCP/epoll,select/poll,AIO ,NIO ,BIO的总结
- 【leetcode】Factorial Trailing Zeroes
- [PHP学习教程 - 文件]001.高速读写大数据“二进制”文件,不必申请大内存(Byte Block)
- [大数据处理]-如何用消息系统避免分布式事务?
- 【重磅】纲要已出 大数据行业全面解析
- HDU4355-Party All the Time-三分法(模板)-2012 Multi-University Training Contest 6
- shutdownServer failed: shutdown must run from localhost when running db without auth
- 国务院:《促进大数据发展行动纲要》-全文