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 两数相加
给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...
随机推荐
- 2016.4.6 WinForm显示PDF两种方法
1.最直接的方法,添加webbrowser控件 webb.Url = new Uri(path);可显示pdf控件. 如果需要在打开时跳转到某页,可用在路径后直接加#page=,例如webb.Url ...
- IOCP编程原理(转)
在我的博客之前写了很多关于IOCP的“行云流水”似的看了让人发狂的文章,尤其是几篇关于 IOCP加线程池文章,更是让一些功力不够深厚的初学IOCP者,有种吐血的感觉.为了让大家能够立刻提升内力修为,并 ...
- JVM Class Loading过程
转自:<Java Performance>第三章 VM Class Loading The Hotspot VM supports class loading as defined by ...
- leetcode637
层次遍历,计算每一层的节点值,然后求平均值. class Solution { public: vector<double> averageOfLevels(TreeNode* root) ...
- leetcode319
public class Solution { public int BulbSwitch(int n) { var x = Math.Sqrt(n); var y = Convert.ToInt32 ...
- Android Studio 第一次配置及其使用
第一次使用Android Studio时你应该知道的一切配置 http://www.cnblogs.com/smyhvae/p/4390905.html gradle V2.10 版: http:// ...
- java GC是在什么时候,对什么东西,做了什么事情?
1.新生代有一个Eden区和两个survivor区,首先将对象放入Eden区,如果空间不足就向其中的一个survivor区上放,如果仍然放不下就会引发一次发生在新生代的minor GC,将存活的对象放 ...
- PHP trim() 函数
定义和用法 trim() 函数从字符串的两端删除空白字符和其他预定义字符. 语法 trim(string,charlist) 参数 描述 string 必需.规定要检查的字符串. charlist 可 ...
- matlab rand(3,5)
rand()函数在(0,1)上创建均匀分布的随机数的数组 >> rand(3,5) ans = 0.8147 0.9134 0.2785 0.9649 0.9572 0.9058 0.63 ...
- 算法Sedgewick第四版-第1章基础-2.3 Quicksort-001快速排序
一. 1.特点 (1)The quicksort algorithm’s desirable features are that it is in-place (uses only a small a ...