2. Add Two Numbers(2个链表相加)
You are given two non-empty linked lists representing two non-negative integers. 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.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
20180223
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode fakehead = new ListNode(0);
ListNode prev = fakehead;
int carry = 0;
for(ListNode p1=l1, p2 = l2;
p1!=null || p2!=null;
p1=(p1==null?null:p1.next),p2=(p2==null?null:p2.next)
){
int p1val = p1==null?0:p1.val;
int p2val = p2==null?0:p2.val;
int val = p1val+p2val+carry;
carry = val/10;
val = val%10;
ListNode temp = new ListNode(val);
prev.next = temp;
prev = prev.next;
}
if(carry>0)
prev.next = new ListNode(carry);
return fakehead.next;
}
}
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
// ListNode cur =new ListNode(0);
ListNode prev= new ListNode(0);
ListNode head = prev;
int pval;
int jinwei=0;
while(l1!=null ||l2!=null || jinwei!= 0 ){
pval = ((l2 == null) ? 0 : l2.val) + ((l1 == null) ? 0 : l1.val) + jinwei;
if(pval>9) { jinwei =1;pval=pval-10; }
else jinwei=0;
ListNode cur = new ListNode(pval);
prev.next =cur;
prev = cur;
l1 = (l1 == null) ? l1 : l1.next;
l2 = (l2 == null) ? l2 : l2.next; }
return head.next; } }
2. Add Two Numbers(2个链表相加)的更多相关文章
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- [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 ...
- [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现
[LeetCode] Add Two Numbers 两个数字相加 You are given two non-empty linked lists representing two non-ne ...
- [LeetCode] 2. Add Two Numbers 两个数字相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- [LintCode] Add Two Numbers 两个数字相加
You have two numbers represented by a linked list, where each node contains a single digit. The digi ...
- 2.Add Two Numbers-两个单链表相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- [leetcode]445. Add Two Numbers II 两数相加II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
- 2. Add Two Numbers[M]两数相加
题目 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...
- 445 Add Two Numbers II 两数相加 II
给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表.你可以假设除了数字 0 之外,这两个数字都不会以零开头.进阶:如果输入链表 ...
随机推荐
- BI产品学习笔记
理解现在--挖掘规律--预测未来------------------------------------------------------精准营销智能风控运营优化 多维分析挖掘预测敏捷BI 分析展示 ...
- python2.0_s12_day21_web聊天室一
本节内容: 项目实战:开发一个WEB聊天室 功能需求: 用户可以与好友一对一聊天 可以搜索.添加某人为好友 用户可以搜索和添加群 每个群有管理员可以审批用户的加群请求,群管理员可以用多个,群管理员可以 ...
- laravel 社会化(联合)登录扩展包(QQ、微信、微博等)
laravel的官方包只支付国外网站的联合登录:http://laravelacademy.org/post/6288.html 国内用户的话,可以用这个:https://github.com/ove ...
- glob模块--查询一个文件名列表
''' 在python中,glob模块是用来查找匹配的文件的 在查找的条件中,需要用到Unix shell中的匹配规则: * : 匹配所所有 ? : 匹配一个字符 *.* : 匹配如:[hello.t ...
- Ubuntu13.10:[3]如何开启SSH SERVER服务
作为最新版本的UBUNTU系统而言,开源,升级全部都不在话下.传说XP已经停止补丁更新了,使用UBUNTU也是一个很好的选择.ubuntu默认安装完成后只有ssh-agent(客户端模式),宾哥百度经 ...
- android基础---->AIDL服务的使用
AIDL和其他的IDL类似,它允许你定义程序接口,以便客户端与服务器端通过IPC机制交互.在android上面,一个进程一般不能访问另外进程的内存.因此,Android平台将这些跨进程访问的对象分解成 ...
- 【Android M】获取屏幕锁定的相关信息:“无”,“滑动”,“PIN码”,"图案","密码"
ENV: Android M 6.0.1 import android.os.UserHandle; import com.android.internal.widget.LockPa ...
- Python - 3.6 学习第一天
开始之前 基础示例 Python语法基础,python语法比较简单,采用缩紧方式. # print absolute value of a integer a = 100 if a >= 0: ...
- python随机验证码函数
#验证码函数def yzm(i): code = [] for i in range(i): ,): code.append(str(random.randint(,))) else: tmp = r ...
- MySQL 5.6 my.cnf 参数说明(转)
# 以下选项会被MySQL客户端应用读取. # 注意只有MySQL附带的客户端应用程序保证可以读取这段内容. # 如果你想你自己的MySQL应用程序获取这些值. # 需要在MySQL客户端库初始化的时 ...