题目描述:

解题思路:

  给定两个链表(代表两个非负数),数字的各位以倒序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和)。

  如(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★★的更多相关文章

  1. 【LeetCode445】 Add Two Numbers II★★

    题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...

  2. 【leetcode】Add Two Numbers

    题目描述: You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  3. 【题解】【链表】【Leetcode】Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  4. 【leetcode】Add Two Numbers(middle) ☆

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  5. 【leetcode】 Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  6. 【链表】Add Two Numbers

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  7. 【Leetcode】【Medium】Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  8. 【LeetCode】Add Two Numbers(两数相加)

    这道题是LeetCode里的第2道题. 题目要求: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将 ...

  9. 【LeetCode67】 Add Binary

    题目描述: 解题思路: 此题的思路简单,下面的代码用StringBuilder更加简单,注意最后的结果要反转过来.[LeetCode415]Add Strings的解法和本题一模一样. java代码: ...

随机推荐

  1. Java设计模式—建造者模式

    建造模式:        将一个复杂的对象的构建与它的表示分离,使得同样的构建 过程可以创建不同的. 建造模式表示是将复杂的内部创建封装在内部,对于外部调用的人来说,只需要传入建造者和建造工具,对于内 ...

  2. 润乾在东方通tongweb5.0上部署手册

     作为国内领先的中间件开发商,东方通是国内最早研究J2EE技术和开发应用服务器产品的厂商.应用服务器TongWeb的开发目标,是利用公司在中间件 领域的技术优势,实现符合J2EE规范的企业应用支撑 ...

  3. Object equals 方法

    package com.mydemo.controller; public class TestEquals { public static void main(String[] args) { Do ...

  4. JS JSON序列化 Ajax form表单

    # JS序列化 a = {"k1":"v1"} #序列化为字符串 类似python json.dumps(a) b = JSON.stringify(a) &q ...

  5. CSS深入理解之overflow(HTML/CSS)

    简介 overflow看上去其貌不扬,其中蕴含的知识点还是很多的,有很多鲜为人知的特性表现. overflow基本属性值 1.visible(默认) 2.hidden 3.scroll 4.auto ...

  6. mysql常用语句备忘

    1.连接本地数据库 mysql -h localhost -u root -p123 2.连接远程数据库 mysql -h 192.168.0.201 -P 3306 -u root -p123 3. ...

  7. MyEclipse中修改servlet模板

    1.在MyEclipse目录下搜索com.genuitec.eclipse.wizards,得到搜索结果 com.genuitec.eclipse.wizards_8.4.100.me20091213 ...

  8. [翻译] AAPullToRefresh

    AAPullToRefresh 效果: Requirement - 需要的环境 ARC. iOS 6 or higher(tested on iOS 6, 7 and 8). Install - 安装 ...

  9. [C++] 用Xcode来写C++程序[3] Constants

    用Xcode来写C++程序[3] Constants 以下是一些基本数据的含义: 75 // int 75u // unsigned int 75l // long 75ul // unsigned ...

  10. python操作Exchange邮箱实例(-)

    需求很简单,就是实现按公司域名及服务器模拟exchange发送邮件,主要是协助自动化测试.主要功能:收件人/抄送/正文html/附件 本实例基于:python2.7.11 exchangelib1.1 ...