LeetCode 2 Add Two Numbers
2016-02-27 23:38
344 查看
原创:http://blog.csdn.net/u013383042/article/details/50757591
题目:
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.
题目:
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.
/** * 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) { int carry = 0; ListNode listNode = new ListNode(0); ListNode p1 = l1,p2 = l2,p3 = listNode; while(p1 != null || p2 != null) { if(p1 != null) { carry += p1.val; p1 = p1.next; } if(p2 != null) { carry += p2.val; p2 = p2.next; } p3.next = new ListNode(carry % 10); p3 = p3.next; carry /= 10; } if(carry == 1) { p3.next = new ListNode(1); } return listNode.next; } }
相关文章推荐
- hdoj 5634 Rikka with Phi 【线段树 + 欧拉】
- hdoj 5631 Rikka with Graph 【并查集】
- 基于Python的数据可视化 matplotlib seaborn pandas
- 论下载程序升级包在手机移动网和WiFi的不同
- 设置mysql的最大连接数
- 【MOOC EXP】Linux内核分析实验一报告
- 一批违规网络大V账号被关闭
- 一批违规网络大V账号被关闭
- python中map()与zip()操作方法
- rsync+inotify实现数据实时同步
- Android系统之G-sensor
- css学习(一)-CSS基础、css选择器
- AFN3.0与2.0分别做了哪些处理
- hdoj 5630 Rikka with Chess 【水题】
- Unity3D 5 官方教程:SpeedTree/LOD Trees
- Notepad++插件Emmet不工作解决办法
- python之字符串
- android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity
- NYOJ 某种序列
- 【线段树】【二分】[PA2015][BZOJ4293]Siano