【LeetCode2】Add Two Numbers★★
题目描述:

解题思路:
给定两个链表(代表两个非负数),数字的各位以倒序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和)。
如(2->4->3)(342) + (5->6->4)(465) = (7->0->8)(807)
设两个进行加法运算的链表分别为l1,l2, 结果链表为resultList,以l1[i] 表示链表l1的第i个节点的值,l2[i] 表示链表l2的第i个节点的值,carry[i]表示l[i]位相加产生的进位符。
则有以下结论:
当链表l1和l2不同时为空时:
resultList[i] = (l1[i] + l2[i] + carry[i-1]) % 10
carry[i] = (l1[i] + l2[i] + carry[i-1]) / 10
且carry[0] = 0;
Java代码:
//类public class LeetCode2为测试代码
public class LeetCode2{
public static void main(String[] args) {
ListNode l1=new ListNode(2),l11=new ListNode(4),l12=new ListNode(3);
l1.next=l11;
l11.next=l12;
System.out.print("Input:["+l1.val+","+l11.val+","+l12.val+"]");
ListNode l2=new ListNode(5),l21=new ListNode(6),l22=new ListNode(4);
l2.next=l21;
l21.next=l22;
System.out.println(",["+l2.val+","+l21.val+","+l22.val+"]");
ListNode list=new Solution().addTwoNumbers(l1, l2);
if(list!=null)
System.out.print("output:["+list.val);
while(list.next!=null){
System.out.print(","+list.next.val);
list.next=list.next.next;
}
System.out.println("]");
}
}
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode resultList=new ListNode(0);
ListNode p1=l1,p2=l2,p3=resultList;
int carry=0;
while(p1!=null||p2!=null){
if(p1!=null){
carry+=p1.val;
p1=p1.next;
}
if(p2!=null){
carry+=p2.val;
p2=p2.next;
}
p3.next=new ListNode(carry%10);
p3=p3.next;
carry/=10;
}
if(carry==1)
p3.next=new ListNode(1);
return resultList.next;
}
}
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
程序结果:

【LeetCode2】Add Two Numbers★★的更多相关文章
- 【LeetCode445】 Add Two Numbers II★★
题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...
- 【leetcode】Add Two Numbers
题目描述: You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- 【题解】【链表】【Leetcode】Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【leetcode】Add Two Numbers(middle) ☆
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【leetcode】 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【链表】Add Two Numbers
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- 【Leetcode】【Medium】Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【LeetCode】Add Two Numbers(两数相加)
这道题是LeetCode里的第2道题. 题目要求: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将 ...
- 【LeetCode67】 Add Binary
题目描述: 解题思路: 此题的思路简单,下面的代码用StringBuilder更加简单,注意最后的结果要反转过来.[LeetCode415]Add Strings的解法和本题一模一样. java代码: ...
随机推荐
- Jquery插件网站持续添加。。。
Look Fro Less,Do More www.jq22.com
- JavaScript 面向对象的程序设计
面向对象(Object-oriented,OO)的语言有一个标志,那就是它们都有类的概念.而通过类可以创建任意多个具有相同属性和方法的对象.前面提到过,ECMAScript中没有类的概念,因此它的对象 ...
- phoenix使用vue--单独js(不使用app.js)
实际中不能都在一个js里 api.js app.js admin.js --vue 后台 记录下方法 static--admin--hello.js import "phoenix_html ...
- Linux学习之CentOS(四)----Linux各目录的介绍
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...
- leetCode题解之寻找插入位置
1.问题描述 Search Insert Position Given a sorted array and a target value, return the index if the targe ...
- 1.Junit test使用
1.导入maven依赖 <dependency> <groupId>junit</groupId> <artifactId>junit</arti ...
- 设计标签选择器TitleSwitch
设计标签选择器TitleSwitch 效果如下: 源码如下: TitleSwitch.h 与 TitleSwitch.m // // TitleSwitch.h // TitleSwitch // / ...
- Linux 隐藏系统信息
Linux查看系统信息 [更多参考]https://www.cnblogs.com/ftl1012/p/uname.html Linux隐藏系统信息 查看: cat /etc/issue.net ...
- zabbix日常监控NFS(新加一)
有时候主机使用NFS文件挂载的方式来存储.备份.共享文件:但有时会出现断开的现象. 1.客户机现状 [root@tianxia6 ~]# df -h Filesystem Size Used Avai ...
- MVC5开发环境的配置
如果你打算在VS2012上开发MVC5,请通过WPI来安装此组件:ASP.NET and Web Tools 2013.1 version