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 之外,这两个数字都不会以零开头.进阶:如果输入链表 ...
随机推荐
- 端口占用处理(Windows)
1. netstat -ano 看看 443端口被占用没 ,一般是本地地址加上端口号,那个端口号就是你要找的,同时最后一列是PID,即进程号. (常用的软件有虚拟机.VisualSVNServer)2 ...
- 06python 之基本数据类型
数字 int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31~2**31-1,即-2147483648~2147483646 在64位机器上,整数的位数为64位,取值范围为-2** ...
- hadoop程序MapReduce之DataDeduplication
需求:去掉文件中重复的数据. 样板:data.log 2016-3-1 a 2016-3-2 b 2016-3-2 c 2016-3-2 b 输出结果: 2016-3-1 a 2016 ...
- [原创]用Oreans UnVirtualizer还原VM代码
标题:[原创]用Oreans UnVirtualizer还原VM代码实验 作者:sungy 时间:2014-09-18 对VM一直很头痛,在逆向实践一般尽量想办法避开它,当黑盒来处理.有时候不面对她又 ...
- LeetCode——Palindrome Linked List
Description: Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it ...
- 图的建立(邻接矩阵)+深度优先遍历+广度优先遍历+Prim算法构造最小生成树(Java语言描述)
主要参考资料:数据结构(C语言版)严蔚敏 ,http://blog.chinaunix.net/uid-25324849-id-2182922.html 代码测试通过. package 图的建 ...
- vue报错一
8080端口被占用 解决方案: 打开cmd输入:netstat -ano查看所有端口信息,如图,找到端口 8080,以及对应的 PID: 输入:tskill PID 即可杀死进程,ex:: tskil ...
- 【Android】Could not find XXX.apk!的解决方法
昨天在Eclipse中导入一个Android工程后点击运行时出现了Could not find XXX.apk!的错误信息,具体错误提示如下: 到网上搜了好多方法,挨个尝试,最后都没解决但是,重启 ...
- innodb的innodb_buffer_pool_size和MyISAM的key_buffer_size(转自:http://www.java123.net/898181.html)
一. key_buffer_size 对MyISAM表来说非常重要. 如果只是使用MyISAM表,可以把它设置为可用内存的 -%.合理的值取决于索引大小.数据量以及负载 -- 记住,MyISAM表会使 ...
- 树形DP求树的直径
hdu4607 Park Visit Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...