(LinkedList)2. Add Two Numbers
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) {
if (l1 == null)
return l2;
if (l2 == null) //这题看起来简单,但是花了很长时间啊,链表要遍历几次,但是这样写代码最短?实在不想再写了
return l1;
int temp = 0;
ListNode p = l1;
ListNode q = l2;
ListNode longList = l1;
ListNode la = l1;
while (p != null && q != null) {
p = p.next;
q = q.next;
}
if (q == null) {
q = l1;
p = l2;
longList = l1;
} else {
q = l2;
p = l1;
longList = l2;
}
while (p != null && q != null) {
if (p.val + q.val + temp >= 10) {
q.val = p.val + q.val + temp - 10;
temp = 1;
} else {
q.val = q.val + p.val + temp;
temp = 0;
}
p = p.next;
q = q.next;
}
while (q != null) {
if (q.val + temp >= 10) {
q.val = q.val + temp - 10;
temp = 1;
} else {
q.val = q.val + temp;
temp = 0;
}
q = q.next;
}
if (temp == 1) {
ListNode last = new ListNode(1);
last.next = null;
p = longList;
while (p.next!= null)
p = p.next;
la = p;
la.next = last;
} return longList;
}
}
(LinkedList)2. Add Two Numbers的更多相关文章
- Add Two Numbers I & II
Add Two Numbers I You have two numbers represented by a linked list, where each node contains a sing ...
- [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; ...
随机推荐
- [转载]四大Java EE容器
转载自: https://my.oschina.net/diedai/blog/271367 现在流行的Java EE容器有很多:Tomcat.JBoss.Resin.Glassfish等等.下面对这 ...
- 关于DButils的简单介绍
android中的orm框架,一行代码就可以进行增删改查:支持事务,默认关闭:可通过注解自定义表名,列名,外键,唯一性约束,NOT NULL约束,CHECK约束等(需要混淆的时候请注解表名和列名)等等 ...
- 服务器端与客户端TCP连接入门(三:多线程)
对于服务器端来说,如果要加入多线程机制,则应该在每个用户连接之后启动一个新的线程 建立一个EchoThread类,此类专门用于处理多线程操作,此时的多线程使用Runnable接口实现 package ...
- HDFS中的checkpoint( 检查点 )的问题
1.问题的描述 由于某种原因,需要在原来已经部署了Cloudera CDH集群上重新部署,重新部署之后,启动集群,由于Cloudera Manager 会默认设置dfs.namenode.checkp ...
- C#代码示例_函数
参数数组 C#允许为函数指定一个(只能指定一个)特定的参数,这个参数必须是函数定义中的最后一个参数,称为参数数组.参数数组可以使用个数不定的参数调用函数,可以使用params关键字定义它们. 参数数组 ...
- 查看oracle当前session
怎样查看oracle当前的连接数呢?只需要用下面的SQL语句查询一下就可以了. #查看当前不为空的连接select * from v$session where username is not n ...
- 网络编程-socket
本节内容: 一:TCP/IP:Transmission Control Protocol/Internet Protocol 传输控制协议/因特网互联协议.即通讯协议.是主机接入互联网以及互联网中两台 ...
- Jena Fuseki 102
Version Fuseki v1 Fuseki v2 since Jena 2.13.0 Both v1 and v2 are active and maintained.[2015/06/29] ...
- 使用curl获取Location:重定向后url
在php获取http头部信息上,php有个自带的函数get_headers(),我以前也是用这个的,听说效率在win上不咋地,再加上最近研究百度url无果,写了cURL获取重定向url的php代码来折 ...
- Docker SSH
1. Dockerfile -->docker build -t centos6-ssh https://git.oschina.net/feedao/Docker_shell/raw/st ...