[LeetCode]Add Two Numbers
2015-10-08 16:37
465 查看
题目:点击打开链接
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { if ((null == l1) && (null == l2)) { return null; } ListNode result = null; ListNode node = null; int carry = 0; while ((null != l1) && (null != l2)) { int value = l1.val + l2.val + carry; carry = value / 10; value = value % 10; if (null == node) { result = new ListNode(value); node = result; } else { node.next = new ListNode(value); node = node.next; } l1 = l1.next; l2 = l2.next; } while (null != l1) { int value = l1.val + carry; carry = value / 10; value = value % 10; node.next = new ListNode(value); node = node.next; l1 = l1.next; } while (null != l2) { int value = l2.val + carry; carry = value / 10; value = value % 10; node.next = new ListNode(value); node = node.next; l2 = l2.next; } if (0 != carry) { node.next = new ListNode(carry); node = node.next; } return result; } }
相关文章推荐
- oracle的乐观锁和悲观锁
- LeetCode(92) Reverse Linked List II
- managed_shared_memory.construct造成的性能损失
- 6-1、全排序 --(按字典顺序)BinaryComparable
- 公式
- 韩顺平java 第29讲 swing介绍
- React使用指南
- RTP协议
- iOS---Block分析
- 主表和子表主键设计
- 1.1 Introduction (computer abstractions and technology)
- cocos代码研究(17)Widget子类RadioButtonGroup学习笔记
- 手动安装gem2.4.8
- 查看和修改环境变量PATH
- 客户端中webview的网页判断是不是在客户端内部,还是在浏览器打开
- Linksys 发布开源路由器 WRT1900ACS
- OpenWrt下编写AdHoc模式系统自启动脚本
- php面向对象编程学习之高级特性
- linux系统安装
- 联动菜单