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 计算两个对应的列表和问题的更多相关文章

  1. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  2. LeetCode 445. Add Two Numbers II (两数相加 II)

    题目标签:Linked List 题目给了我们两个 数字的linked list,让我们把它们相加,返回一个新的linked list. 因为题目要求不能 reverse,可以把 两个list 的数字 ...

  3. leetcode 题解 Add Two Numbers(两个单链表求和)

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  4. [leetcode]445. Add Two Numbers II 两数相加II

    You are given two non-empty linked lists representing two non-negative integers. The most significan ...

  5. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  6. LeetCode:1. Add Two Numbers

    题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...

  7. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  8. [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现

    [LeetCode] Add Two Numbers 两个数字相加   You are given two non-empty linked lists representing two non-ne ...

  9. [LeetCode] 2. Add Two Numbers 两个数字相加

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

随机推荐

  1. MYSQL正在使用select发现现场记录方法,包括一个逗号分隔的字符串

    首先,我们创建一个逗号分隔字符串. CREATE TABLE test(id int(6) NOT NULL AUTO_INCREMENT,PRIMARY KEY (id),pname VARCHAR ...

  2. 在html中禁用自己主动完毕

    输入框输入内容时总是显示历史输入历史记录,现禁用的方法是加入一个属性: <input type="text name="txt_xm" autocomplete=& ...

  3. Nagios经check_http监视web申请书server多个tomcat维修

    怎么样nagios显示器tomcat,它是一个相对简单的和复杂的事情.简单是因为,只有监控的假设web应用服务器tomcat无论是服务正常进行,很简单.假设你要监视tomcat其他例子,例如连接数jv ...

  4. hdu1520 (树形dp)

    hdu1520 http://acm.hdu.edu.cn/showproblem.php?pid=1520 题意是给定一棵树,每个结点有一个价值,要我们选择任意个结点使得总价值最大,规则是如果父亲结 ...

  5. 大爱jQuery,10美女模特有用jQuery/CSS3插入(集成点免费下载)

    整合下载地址:http://download.csdn.net/detail/yangwei19680827/7343001 jQuery真的是一款非常犀利的Javascript框架,利用jQuery ...

  6. Red Gate系列之二 SQL Source Control 3.0.13.4214 Edition 数据库版本控制器 完全破解+使用教程

    原文:Red Gate系列之二 SQL Source Control 3.0.13.4214 Edition 数据库版本控制器 完全破解+使用教程 Red Gate系列之二 SQL Source Co ...

  7. C#项目开发实践前言

    曾经没有做过项目开发实现解说,都是在工作过程其中,自动学习,查找资料,由于在曾经的公司就我一人在做c#WinForm开发,所以,有时候在公司培训会上,我也会为新的员工进行过一些简单的项目解说,基于在培 ...

  8. 【原创】leetCodeOj --- Word Ladder II 解题报告 (迄今为止最痛苦的一道题)

    原题地址: https://oj.leetcode.com/submissions/detail/19446353/ 题目内容: Given two words (start and end), an ...

  9. hdu 4268 Alice and Bob(multiset|段树)

    Alice and Bob Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  10. 2015华为德州扑克入境摘要——软体project

    直到6一个月2号下午12时00,华为长达一个月的德州扑克锦标赛落下帷幕也被认为是. 我们的团队一直共同拥有3民,间.一个同学(吴)负责算法设计,一个同学(宋)负责分析消息,而我负责的实现框架设计和详细 ...