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 ...
随机推荐
- 客户端获取服务端自定义类数据 z
客户端获取服务端自定义类数据 问题一:超时问题,在最后获取数据的时候突然提示服务超时,服务已断开 解决:配置文件添加: <bindings> <wsHttpBinding> & ...
- 怎样查看oracle当前的连接数
SQL> select count(*) from v$session #当前的连接数SQL> Select count(*) from v$session where status='A ...
- C#导出带有格式的Excel(列宽,合并单元格,显示边框线,加背景颜色等)
源地址:http://blog.sina.com.cn/s/blog_74f702e60101au55.html 导出excel相关设置:http://blog.csdn.net/wanmingtom ...
- [Java] java中方法可以重载
一个类中可以定义不止一个构造器,在使用new创建对象时,Java会根据构造器提供的参数来决定构建哪一个构造器,另外在Java中,Java会同时根据方法名和参数列表来决定所要调用的方法,这叫做方法重载( ...
- Oracle中对表的操作
表的创建与管理 创建表: CREATE TABLE TABLE_NAME ( COLUMN_NAME TYPE [DEFAULT VALUE], COLUMN_NAME TYPE [DEFAULT V ...
- 山东省第五届ACM省赛
题目链接:http://acm.sdut.edu.cn/sdutoj/contest_show.php?contest_id=1449 相关总结:http://www.cnblogs.com/mcfl ...
- poj 2253 Frogger dijkstra算法实现
点击打开链接 Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21653 Accepted: 7042 D ...
- (easy)LeetCode 198.House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- 使用spring的邮件发送功能
使用spring提供的MailSender和JavaMailSender类. 1.邮件对象类 package cn.luxh.app.mail; import java.util.List; impo ...
- Ubuntu 12.04 gedit编辑器 中文乱码
百度一下查看了很多关于这个问题的解决方法,无非是用通过配置编辑器修改键值来解决.但是由于我的ubuntu是12.04版本的,搜索到的很多方法都不能用,网上一般的解决办法如下: 打开“注册表”(从字面理 ...