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 ...
随机推荐
- Yii 框架中安装 memcache 及配置和测试
安装php_memcache.dll扩展 1.首先将php_memcache.dll文件放入E:\server\php\ext目录下 (php_memcache.dll下载地址:http://wind ...
- jsp的一些基本语法
jsp页面内容 <%@ page language="java" import="java.util.*" pageEncoding="UTF- ...
- iOS 图片拉伸 resizableImageWithCapInsets
UIImage *image = [[UIImage imageNamed:@"test.png"] resizableImageWithCapInsets:UIEdgeInse ...
- DXperience-11.1.5 破解
将DXPerience_11.1.5_Crack里的所有文件粘贴到DXperience-11.1.5的bin文件夹下,然后在cmd运行register.bin
- 【在线】Actionbar Style Generator:ActionBar风格生成器
这个ActionBar风格生成器可以让你轻松地创建一个简洁.有吸引力且无漏洞的自定义actionbar.它会生成所有9种必须的patch assets以及相关XML的drawables和styles文 ...
- NAND flash cache编程
PROGRAM PAGE CACHE MODE 0x80-0x15: CACHE编程实际上是标准的页编程命令的带缓冲编程模式,编程开始是发布SERIAL DATA INPUT(0x80)命令,随后是5 ...
- Mingyang.net:注解配置Hibernate时报错Unknown Entity
注解配置时报错:org.hibernate.MappingException: Unknown entity: net.mingyang.cms.bean.User org.hibernate.Map ...
- (转)C#精确时间计时器
原文地址:http://blog.sina.com.cn/s/blog_699d3f1b01012vgb.html 1 调用WIN API中的GetTickCount [DllImport(" ...
- 多线程为何用while判断条件,而不用if。
一.代码: package zz.produceandconsumer; import java.util.LinkedList; public class Storage { private fin ...
- g++/gcc 链接头文件 库 PATH
转自http://blog.csdn.net/kankan231/article/details/24243871 在Linux下编译链接或运行c/c++程序时可能会遇到找不到头文件,找不到库文件的错 ...