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 ...
随机推荐
- Android的PackageManager的使用
Android系统提供了很多服务管理的类,包括ActivityManager.PowerManager(电源管理).AudioManager(音频管理)以及PackageManager管理类.Pack ...
- JAVA 读取图片储存至本地
需求:serlvet经过处理通过报表工具返回一张报表图(柱状图 折线图). 现在需要把这个图存储到本地 以便随时查看 // 构造URL URL url = new URL(endStr); // 打开 ...
- Troubleshooting "Global Enqueue Services Deadlock detected" (Doc ID 1443482.1)
In this Document _afrLoop=1021148011984950&id=1443482.1&displayIndex=1&_afrWindowMode= ...
- CodeForces 343D 线段树维护dfs序
给定一棵树,初始时树为空 操作1,往某个结点注水,那么该结点的子树都注满了水 操作2,将某个结点的水放空,那么该结点的父亲的水也就放空了 操作3,询问某个点是否有水 我们将树进行dfs, 生成in[u ...
- hadoop的一些名词解释
在网上收集了一些mapreduce中常用的一些名词的解释,分享一下: Shuffle(洗牌):当第一个map任务完成后,节点可能还要继续执行更多的map 任务,但这时候也开始把map任务的中间输出交换 ...
- kb3035583
dism /online /Get-Packages /Format:Table|findstr 3035583 升级到w10补丁
- 完全合并C++面试题
C++面试题 1.是不是父母写了virtual 功能,假设子类重写它的功能不virtual ,也使多态性? virtual修饰符隐形遗传. private 还集成.问权限而已 virtual可加可不加 ...
- ios01
http://code4app.com/ 0.全部Swift http://numbbbbb.gitbooks.io/-the-swift-programming-language-/content/ ...
- 玩转web之json(五)---将表单通过serialize()方法获取的值转成json
form表单有一个serialize()方法,可以序列化表单的值,但是jquery提供的这个方法会把数据序列化为类似下面的形式: a=1&b=2&c=3&d=4 jquery并 ...
- C# 通过豆瓣网络编程API获取图书信息
这篇文章主要是关于如何通过豆瓣API获取信息的书籍,起初,我看到了原来的想法的内容是"C# 网络编程之网页简单下载实现"中通过HttpWebResponse类下载源代码,再通过正則 ...