[Algo] 223. Add Two Numbers
/**
* class ListNode {
* public int value;
* public ListNode next;
* public ListNode(int value) {
* this.value = value;
* next = null;
* }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
// Write your solution here
ListNode dummy = new ListNode(0);
ListNode cur = dummy;
int sum = 0;
while (l1 != null || l2 != null) {
if (l1 != null) {
sum += l1.value;
l1 = l1.next;
}
if (l2 != null) {
sum += l2.value;
l2 = l2.next;
}
cur.next = new ListNode(sum % 10);
cur = cur.next;
sum = sum / 10;
}
if (sum == 1) {
cur.next = new ListNode(1);
}
return dummy.next;
}
}
[Algo] 223. Add Two Numbers的更多相关文章
- [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 ...
- 52. 不用+、-、×、÷做加法[add two numbers without arithmetic]
[本文链接] http://www.cnblogs.com/hellogiser/p/add-two-numbers-without-arithmetic.html [题目] 写一个函数,求两个整数的 ...
- Leetcode-2 Add Two Numbers
#2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...
- [CareerCup] 2.5 Add Two Numbers 两个数字相加
2.5 You have two numbers represented by a linked list, where each node contains a single digit. The ...
- [LintCode] Add Two Numbers 两个数字相加
You have two numbers represented by a linked list, where each node contains a single digit. The digi ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- [LeetCode_2] Add Two Numbers
LeetCode: 2. Add Two Numbers /** * Definition for singly-linked list. * struct ListNode { * int val; ...
- Two Sum & Add Two Numbers
Two Sum 题目:https://leetcode.com/problems/two-sum/ class Solution(object): def twoSum(self, nums, tar ...
随机推荐
- 后端使用aes 加密
package com.util; /* import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;*/ import org.apa ...
- BZOJ:2190: [SDOI2008]仪仗队
题解:欧拉函数 #include<iostream> #include<cstdio> #include<cstring> using namespace std; ...
- 《新标准C++程序设计》1.1-1.6(C++学习笔记1)
1.cout输出 cout<<待输出项<<待输出项2<<···; 2.cin输入 cin>>变量1>>变量2>>···; 3.C ...
- 送票啦~ | 京东云邀您参加AI顶级盛会GTC CHINA 2019
本年度不可错过的AI顶级盛会 GTC CHINA2019 即将于12月16–19日在苏州举行 京东云重量级技术专家将携 AI前沿热议话题亮相 京东云相关AI最新动态,也会一并为您带上 小小剧透,快来看 ...
- spring boot 异常(exception)处理
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- if case for while
#!/bin/basha=$1if [ $a ] #判断$1是否为空then #非空echo "the input is No:$a"exit 0else #空read -p &q ...
- GFlags 处理内存越界、野指针问题研究
从官方文档可以看到,从gflags的GUI上,我们是无法判断他打开的是full page heap verification 还是Standard page heap verification, 所以 ...
- 创建简单spring boot项目
简介 使用spring boot可以轻松创建独立的,基于Spring框架的生产级别应用程序.Spring boot应用程序只需要很少的spring配置 特点 创建独立的Spring应用程序 直接嵌入t ...
- ZOJ 3765 Lights (zju March I)伸展树Splay
ZJU 三月月赛题,当时见这个题目没辙,没学过splay,敲了个链表TLE了,所以回来好好学了下Splay,这道题目是伸展树的第二题,对于伸展树的各项操作有了更多的理解,这题不同于上一题的用指针表示整 ...
- Red Hat、Fedora和Ubuntu软件包操作
五.Red Hat.Fedora和Ubuntu软件包操作对比 最后总结: 在 /etc/yum.conf 的 [main] 后面添加以下配置即可! 复制代码 代码如下: exclude=kernel* ...