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 ...
随机推荐
- H264 编解码框架简单介绍
阅读完H264/AVC 编解码器的介绍,脑海中仅仅是留下下面三条: 1.H264并没有明白规定一个编解码器怎样实现,仅仅是规定了一个编码后的视频比特流的句法,和该比特流的解码方法,这个与MPEG 类似 ...
- SSL与TLS的区别以及介绍(转)
SSL:(Secure Socket Layer,安全套接字层),位于可靠的面向连接的网络层协议和应用层协议之间的一种协议层.SSL通过互相认证.使用数字签名确保完整性.使用加密确保私密性,以实现客户 ...
- 为应用程序池“XX”提供服务的进程在与 Windows Process Activation Service 通信时出现严重错误
场景 WCF应用程序部署在IIS7中,使用net.tcp协议对外给几百台客户端提供服务,应用程序池不断崩溃重启. 分析过程 在事件查看器中看到的错误信息类似于 为应用程序池“XX”提供服务的进程在与 ...
- zoj Reactor Cooling
Reactor Cooling 无源汇上下界最大流问题. 1.流量平衡. 2.满足上下界 模板题. #include <iostream> #include <queue> # ...
- android选择和裁剪图像拍摄的图像
转载请注明出处:http://blog.csdn.net/allen315410/article/details/39994913 近期从曾经的项目中扒下来一个经常使用的模块.在这里有必要记录一下的. ...
- Windows Phone开发(23):启动器与选择器之CameraCaptureTask和PhotoChooserTask
原文:Windows Phone开发(23):启动器与选择器之CameraCaptureTask和PhotoChooserTask 这两个组件都属于选择器,而且它们也有很多相似的地方,最明显的上一点, ...
- uptime
linux uptime命令主要用于获取主机运行时间和查询linux系统负载等信息.uptime命令过去只显示系统运行多久.现在,可以显示系统已经运行了多长时间,信息显示依次为:现在时间.系统已经运行 ...
- HDU 4982 Goffi and Squary Partition(推理)
HDU 4982 Goffi and Squary Partition 思路:直接从全然平方数往下找,然后推断是否能构造出该全然平方数,假设能够就是yes,假设都不行就是no.注意构造时候的推断,因为 ...
- HDU ACM 1071 The area 定积分计算
分析: 1.求抛物线方程F(x)=a*x^2+b*x+c: 2.求直线方程f(x)=k*x+b. 3.利用定积分计算F(x)-f(x)在x2到x3之间的面积. #include<iostream ...
- Quartz CronTrigger应用
CronTrigger配置格式: 格式: [第二] [支] [小时] [日本] [月] [周] [年] 序号 说明 是否必填 同意填写的值 同意的通配符 1 秒 是 0-59 , ...