20.Add Two Numbers(两个链表的和)
Level:
Medium
题目描述:
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.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
思路分析:
这道题主要考虑相加之后的进位表示,这里用flag变量进行标记,如果flag=true则代表相加有进位,进位只可能是一,这道题
应该将所有的情况都考虑全;
代码:
public class ListNode{
int val;
ListNode next;
public ListNode(int x){
val=x;
}
}
public class Solution{
public ListNode addTwoNumbers(ListNode l1,ListNode l2){
if(l1==null&&l2==null)
return null;
if(l1==null)
return l2;
if(l2==null)
return l1;
boolean flag=false; //有进位设置为true
ListNode pNode=new ListNode(0);
ListNode res=pNode;
while(l1!=null&&l2!=null){
if(l1.val+l2.val>=10){
if(flag==true){ //有进位
pNode.next=new ListNode((l1.val+l2.val)%10+1);
}
else{
pNode.next=new ListNode((l1.val+l2.val)%10);
flag=true; //产生进位
}
}
if(l1.val+l2.val<10){
if(flag==true){ //有进位
if(l1.val+l2.val+1>=10){
pNode.next=new ListNode((l1.val+l2.val+1)%10);
}else{
pNode.next=new ListNode((l1.val+l2.val+1)%10);
flag=false;
}
}else{
pNode.next=new ListNode(l1.val+l2.val);
flag=false;
}
}
l1=l1.next;
l2=l2.next;
pNode=pNode.next;
}
while(l1!=null){
if(flag==true){//有进位
if(l1.val+1==10){
pNode.next=new ListNode(0); //不改变flag的值,保持有进位
}else{
pNode.next=new ListNode(l1.val+1);
flag=false;//没有进位
}
}
else{
pNode.next=new ListNode(l1.val);
flag=false;
}
l1=l1.next;
pNode=pNode.next;
}
while(l2!=null){
if(flag==true){
if(l2.val+1==10){
pNode.next=new ListNode(0);
}else{
pNode.next=new ListNode(l2.val+1);
flag=false;
}
}
else{
pNode.next=new ListNode(l2.val);
flag=false;
}
l2=l2.next;
pNode=pNode.next;
}
if(flag==true){ //最后如果有进位那么还行需要再产生一个节点。
pNode.next=new ListNode(1);
}
return res.next;
}
}
20.Add Two Numbers(两个链表的和)的更多相关文章
- [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 ...
- 【LeetCode每天一题】Add Two Numbers(两链表相加)
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- LeetCode: 2_Add Two Numbers | 两个链表中的元素相加 | Medium
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- [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 ...
- LeetCode(2):Add Two Numbers 两数相加
Medium! 题目描述: 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头 ...
- 【LeetCode】2. Add Two Numbers 两数相加
给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...
随机推荐
- PowerDesigner中批量替换name和code的脚本
无论是cdm还是pdm都可以批量替换.处理.可在Tool-Execute commands-Edit/Run script中编辑运行脚本: 下面的脚本是批量将CDM中实体的用Code替换掉Name O ...
- HtmlHelper(辅助产生HTML之用)
弱类型: 1.使用HTML辅助方法输出超链接 (1)在View中输出ASP.NET MVC的超链接通常会用Html.ActionLink辅助方法,该方法用于产生文字链接,其文字部分会自动进行HTML编 ...
- 10-09C#语言基础
10-09C#语言基础 第一课 一.新项目的建立:打开Visual studio2012,单击“文件→新建项目→模板isualC# Windows 控制台应用程序→确定”即可. 在新建的项目中,首 ...
- C语言学习笔记--指针概念
指针也是一种变量,占有内存空间,用来保存内存地址,在32位系统中指针的占用的内存大小为4个字节 1.*号的意义 (1)在指针声明时,*号表示所声明的变量为指针 (2)在指针使用时,*号表示取指针所指向 ...
- Http 与Https
一个Http请求 DNS域名解析 --> 发起TCP的三次握手 --> 建立TCP连接后发起http请求 --> 服务器响应http请求,浏览器得到html代码 --> 浏览器 ...
- python+requests+excel 接口测试
1.EXCEL文件接口保存方式,如图. 2.然后就是读取EXCEL文件中的数据方法,如下: import xlrd class readExcel(object): def __init__(self ...
- 使用Aspectj 的配置文件方式进行aop操作
- php中使用array_reduce给数组降维
PHP里面最强大的工具,就是数组,它融合了多种数据结构的特点,数组.队列.栈.哈希表等等,而且容器可以兼容各种类型,任意嵌套,简直无所不能.围绕着数组,PHP原生支持了一些列的函数,使得数组在实际编程 ...
- 第2章 构建springboot工程 2-1 构建SpringBoot第一个demo
以后的趋势肯定是以一个微服务为主导的, Spring-Boot的指导 Maven整个环境构建之前的整个项目其实是一个很普通的J2SE项目,它构建完之后会进行重构,重构为Maven的一个项目路径.可以看 ...
- 算法Sedgewick第四版-第1章基础-025-用队列实现unix下的Directory命令
package algorithms.util; /************************************************************************** ...