leetcode2:Add Two Numbers
Add Two Numbers
Total Accepted: 55216 Total Submissions: 249950My Submissions
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.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode newLink= new ListNode(0);
ListNode current=newLink;
int carry =0;
while (l1 != null || l2 != null) {
int a1 = (l1 == null) ? 0 : l1.val;
int a2 = (l2 == null) ? 0 : l2.val;
int sum = a1 + a2 + carry;
carry = sum / 10;
current.next = new ListNode(sum % 10);
l1 = (l1 == null) ? null : l1.next;
l2 = (l2 == null) ? null : l2.next;
current = current.next;
}
if (carry > 0) {
current.next = new ListNode(carry);
}
return newLink.next;
}
}
leetcode2:Add Two Numbers的更多相关文章
- Leetcode-2 Add Two Numbers
#2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...
- Leetcode2:Add Two Numbers@Python
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- leetcode-2 Add Two Numbers 计算两个对应的列表和问题
1.问题描写叙述: You are given two linked lists representing two non-negativenumbers. The digits are sto ...
- LeetCode-2. Add Two Numbers(链表实现数字相加)
1.题目描述 You are given two non-empty linked lists representing two non-negative integers. The digits a ...
- Leetcode2.Add Two Numbers两数相加
给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...
- LeetCode 2. 两数相加(Add Two Numbers)
2. 两数相加 2. Add Two Numbers 题目描述 You are given two non-empty linked lists representing two non-negati ...
- 【LeetCode445】 Add Two Numbers II★★
题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
随机推荐
- SqlServer2005基于已有表创建分区
随着当今数据库的容量越来越快的朝着在大型数据库或超大型数据库的发展,对于数据库中的大 型表以及具有各种访问模式的表的可伸缩性和可管理性运行环境变得尤为重要, SQL server 从 SQL serv ...
- gridview转成EXCEL文件保存(多页)
CompositeLink complink = new CompositeLink(new PrintingSystem()); PrintableComponentLink link = new ...
- eclipse快捷键使用
Eclipse的编辑功能非常强大,掌握了Eclipse快捷键功能,能够大大提高开发效率.Eclipse中有如下一些和编辑相关的快捷键.1. [ALT+/]此快捷键为用户编辑的好帮手,能为用户提供内容的 ...
- SAP
http://www.itpub.net/thread-1328005-1-1.html http://blog.sina.com.cn/s/blog_4b75f26e0100b52a.html SA ...
- POJ 2096 【期望DP】
题意: 有n种选择,每种选择对应m种状态.每种选择发生的概率相等,每种选择中对应的每种状态发生的概率相等. 求n种选择和m种状态中每种至少发生一次的期望. 期望DP好别扭啊.要用倒推的方法. dp[i ...
- 20. Valid Parentheses(stack)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- Appium技术点之解决屏幕无法点击的情况————Python版本
1.导入包: from appium.webdriver.common.touch_action import TouchAction 2.写代码 TouchAction(driver).pop(x= ...
- JDialog窗体
class MyJDialog extends JDialog{ public MyJDialog(JFrame frame){ super(frame,"第一个Dialog窗体 ...
- DNS拦截的处理
在用webSocket来实现长连接时,我们的链接对象使用了域名.但是再某些省份的网络下,发生了DNS拦截.踹改.导致使用某个域名链接,发生连接不上的现象.[解决方案] 在多次尝试原有域名不能使用的情况 ...
- 用了skin皮肤控件之后,报错:容量超出了最大容量 参数名:capacity
http://blog.csdn.net/keenweiwei/article/details/7403869 用了皮肤控件之后,报错:容量超出了最大容量 参数名:capacity MessageBo ...