leetcode-2 Add Two Numbers 计算两个对应的列表和问题
1.问题描写叙述:
You are given two linked lists representing two non-negativenumbers. The digits are stored in reverse order and each of their nodes containa single digit. Add
the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
http://www.programcreek.com/2012/12/add-two-numbers/
链表的定义:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
这里注意考虑多种情况就可以:即考虑两个链表长度不一致,遍历完链表仍有进位的情况。
解法一:较为繁琐的分类讨论方法,避免使用
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if((l1==null)||(l2==null))
{
return null;
} int temp_val = l1.val + l2.val;
int go = temp_val /10;
ListNode result = new ListNode(temp_val%10);
l1 = l1.next;
l2 = l2.next;
ListNode temp = result;
while((l1!=null)&&(l2!=null))
{
temp_val = l1.val + l2.val + go;
ListNode temp2 = new ListNode(temp_val%10);
temp.next = temp2;
temp = temp2;
l1 = l1.next;
l2 = l2.next;
go = temp_val /10;
} while(l1!=null)
{
temp_val = l1.val + go;
ListNode temp2 = new ListNode(temp_val%10);
temp.next = temp2;
temp = temp2;
l1 = l1.next;
go = temp_val /10;
} while(l2!=null)
{
temp_val = l2.val + go;
ListNode temp2 = new ListNode(temp_val%10);
temp.next = temp2;
temp = temp2;
l2 = l2.next;
go = temp_val /10;
} if(go != 0)
{
temp.next = new ListNode(go);
}
return result;
}
}
解法二:较优的解法。但在leetcode上执行时间要劣于前者
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int carry = 0;//表示进位
ListNode head = new ListNode(0);//首前节点,參照C++的尾后节点
ListNode temp = head;
//一直循环至将两个链表遍历全然才退出
while((l1 != null)||(l2 != null))
{
//仅需确定和的几个因子大小就可以;
int first = (l1 != null) ? l1.val : 0;//使用if语句亦可
int second = (l2 != null) ? l2.val : 0; int result = first + second + carry;//每一次循环计算结果
carry = result /10;
ListNode pNode = new ListNode(result % 10);
temp.next = pNode;
temp = pNode;//准备下一循环 if (l1 != null) {
l1 = l1.next;
} if (l2 != null) {
l2 = l2.next;
} }
//还需考虑carray不等于0及仍有进位的情况
if (carry > 0)
{
temp.next = new ListNode(carry);
} return head.next;//注意此返回值
}
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
leetcode-2 Add Two Numbers 计算两个对应的列表和问题的更多相关文章
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- LeetCode 445. Add Two Numbers II (两数相加 II)
题目标签:Linked List 题目给了我们两个 数字的linked list,让我们把它们相加,返回一个新的linked list. 因为题目要求不能 reverse,可以把 两个list 的数字 ...
- leetcode 题解 Add Two Numbers(两个单链表求和)
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- [leetcode]445. Add Two Numbers II 两数相加II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- LeetCode:1. Add Two Numbers
题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [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 ...
随机推荐
- 【Oracle】-【sqlplus / as sysdba登录报错问题】-新用户使用sqlplus / as sysdba登录报错
刚才打开一个别人的测试库,用root登陆了的,sqlplus / as sysdba竟然报错,奇怪,于是在自己的VM中模拟该过程. 新建了一个test用户: [test@liu bin]# ./sql ...
- c++对象指针-01(转载)
1.指向对像的指针在建立对像时,编译系统会为每一个对像分配一定的存储空间,以存放其成员,对像空间的起始地址就是对像的指针.可以定义一个指针变量,用来存和对像的指针.如果有一个类:class Time{ ...
- SVN的命令解析(感觉不错就转了)
本文链接: http://www.php-oa.com/2008/03/12/svnminglingzailinuxxiadeshiyong.html .将文件checkout到本地目录 svn ch ...
- 【前端攻略】:玩转图片Base64编码(转)
引言 图片处理在前端工作中可谓占据了很重要的一壁江山.而图片的Base64编码可能相对一些人而言比较陌生,本文不是从纯技术的角度去讨论图片的base64编码.标题略大,不过只是希望通过一些浅显的论述, ...
- 该Tiled地图制作拿到项目~~这是偷懒,为了直接复制后写来
1.现在,.h声明private: cocos2d::CCSprite* ninja; cocos2d::CCTMXTiledMap* tileMap; 然后.cpp中增加tileMap = CCT ...
- 视频编解码器,bbv 缓冲区溢出和下溢
使用硬件相似数据处理.数据通常未来,形式的处理后,立即出动.所以,一般有一个数据馈送,数据输出,2接口. 实时硬件处理的基本要求.进来的数据,紧接着治疗头发治疗,这需要在很短的时间,好多毫秒以内,才干 ...
- Windows Phone开发(44):推送通知第二集——磁贴通知
原文:Windows Phone开发(44):推送通知第二集--磁贴通知 前面我们说了第一个类型--Toast通知,这玩意儿不知大家是不是觉得很新鲜,以前玩.NET编程应该没接触过吧? 其实这东西绝对 ...
- C++传递函数指针
函数指针是一个很好的类型.因此,您可以编写一个函数,它的一个参数是一个函数指针.然后.在(外部)当函数使用的函数指针参数,来间接调用时调用相应的参数的函数的函数. 因为指针在不同的情况下能够指向不同的 ...
- 搭建solr单机版
solr单机版的搭建 一.solr单机版的搭建 1.运行环境 solr 需要运行在一个Servlet容器中,Solr4.10.3要求jdk使用1.7以上,Solr默认提供Jetty(ja),本教va写 ...
- OpenGL3D迷宫场景设计
近期学习用opengl库来构建一个3D场景,以及实现场景漫游.粒子系统等效果.终于算是是做了一个3D走迷宫游戏吧. 感觉近期学了好多东西,所以有必要整理整理. 一 实现效果 watermark/2/t ...