LeetCode刷题之旅(2)
来源:互联网 发布:js读取手机本地文件 编辑:程序博客网 时间:2024/05/18 12:33
解决代码:
/** * 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) { ListNode head = new ListNode(0); ListNode p = head; if (l1 != null && l2 != null) { int jinWei = 0, value = 0; while (l1 != null && l2 != null) { value = l1.val + l2.val + jinWei; ListNode temp = new ListNode(value % 10); p.next = temp; p = temp; jinWei = value / 10; l1 = l1.next; l2 = l2.next; } while (l1 != null) { value = l1.val + jinWei; ListNode temp = new ListNode(value % 10); p.next = temp; p = temp; jinWei = value / 10; l1 = l1.next; } while (l2 != null) { value = l2.val + jinWei; ListNode temp = new ListNode(value % 10); p.next = temp; p = temp; jinWei = value / 10; l2 = l2.next; } if(jinWei != 0){ ListNode temp = new ListNode(1); p.next = temp; } return head.next; } else { return head.next; } }}
主要就是需要对加法之后的进位进行考虑,很惊奇发现耗时排名居然在前面,开心~
0 0
- LeetCode刷题之旅(2)
- LeetCode刷题之旅(1)
- LeetCode刷题之旅(3)
- LeetCode刷题之旅(4)
- LeetCode刷题之旅(5)
- LeetCode刷题之旅(6)
- LeetCode刷题之旅(7)
- LeetCode刷题之旅(5)
- LeetCode刷题之旅(6)
- LeetCode刷题之旅(7)
- LeetCode刷题之旅(8)
- LeetCode刷题之旅(9)
- LeetCode 刷题之旅
- LeetCode之旅(2)
- LeetCode之旅-2
- Leetcode 刷题之哈希表
- LeetCode之旅(3)
- LeetCode之旅(4)
- POJ 1149 最大流+构图
- 关于eclipse 里面,运行是找不到主类的问题
- 执行ajaxFileUpload发生jQuery.handleError is not a function错误的个人解决方法
- yii2 GridView 日期格式化并实现日期可搜索 案例
- 微软面试100题-58从尾到头输出链表
- LeetCode刷题之旅(2)
- CodeForces - 554B Ohana Cleans Up (模拟)水
- 查看手机,平板的Iafw
- 2016年百度笔试之乘法表
- spark 与 tensorflow
- MySQL 中通过加binary来区分字母的大小写查询
- 二叉排序树的C++实现,包括难点删除
- linux 统计文件的个数
- C语言贪食蛇