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.

  

题目:

用两个链表表示的两个数,求相加之和

Solution1:  For given two lists, keep looping where either one isn't null. When ListNode comes to null, consider its val as 0.

For result list, use a dummy node.

code:

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
/*
Time Complexity: O(max(l1,l2))
Space Complexity: O(max(l1,l2))
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(-1);
ListNode pre = dummy;
ListNode p1 = l1;
ListNode p2 = l2;
int carry = 0;
while(p1 != null || p2 != null){
int x = (p1 != null) ? p1.val : 0;
int y = (p2 != null) ? p2.val : 0;
int sum = carry + x + y;
carry = sum / 10;
// generate the result list
pre.next = new ListNode(sum % 10);
// move three lists pointers
pre = pre.next;
if(p1 != null) p1 = p1.next;
if(p2 != null) p2 = p2.next;
}
// in case last two digit sum > 10
if(carry > 0){
pre.next = new ListNode(carry);
}
return dummy.next;
}
}

[leetcode]2. Add Two Numbers两数相加的更多相关文章

  1. 【LeetCode】Add Two Numbers(两数相加)

    这道题是LeetCode里的第2道题. 题目要求: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将 ...

  2. [LeetCode]2.Add Two Numbers 两数相加(Java)

    原题地址: add-two-numbers 题目描述: 给你两个非空的链表,表示两个非负的整数.它们每位数字都是按照逆序的方式存储的,并且每个节点只能存储一位数字. 请你将两个数相加,并以相同形式返回 ...

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

    题目标签:Linked List, Math 题目给了我们两个 Linked List, 各代表一个数字,不过顺序的反的.让我们把两个数字相加. 和普通的相加其实差不多,只不过变成了 Linked L ...

  4. LeetCode(2):Add Two Numbers 两数相加

    Medium! 题目描述: 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头 ...

  5. [CareerCup] 18.1 Add Two Numbers 两数相加

    18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...

  6. Leetcode2.Add Two Numbers两数相加

    给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...

  7. 【LeetCode】2. Add Two Numbers 两数相加

    给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...

  8. LeetCode(2): 两数相加

    本内容为LeetCode第二道题目:两数相加 # -*- coding: utf-8 -*- """ Created on Sun Mar 10 10:47:12 201 ...

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

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

随机推荐

  1. PythonStudy——列表的常用操作 List of common operations

    # 1.索引取值: 列表名[index] s1 = [1, 3, 2] print(s1[0]) print(s1[-1]) # 2.列表运算: 得到的是新list s2 = [1, 2, 3] pr ...

  2. esxi5 的tart命令使用注意点

    esxi5.0 注意tar命令参数使用和centos6稍微有点不一样,注意下 注意需要把-f参数单独分离出来,紧接着文件.   而不能和cz命令一起用 ~ # touch abc.txt ~ # ec ...

  3. xgboost实例代码

    # -*- coding: utf-8 -*- import xgboost as xgb import csv import jieba jieba.load_userdict('wordDict. ...

  4. Mysql索引会失效的几种情况

    1.如果条件中有or,即使其中有条件带索引也不会使用(这也是为什么尽量少用or的原因): 2.对于多列索引,不是使用的第一部分,则不会使用索引: 3.like查询是以%开头: 4.如果列类型是字符串, ...

  5. [转帖][分享] 关于系统DIY--by 原罪

    http://wuyou.net/forum.php?mod=viewthread&tid=399277&extra=page%3D1 前几天我发了一个帖子<Windows组件w ...

  6. 前端-JavaScript1-5——JavaScript之变量的类型

    5.1 概述 基本类型5种 number          数字类型 string             字符串类型 undefined      undefined类型,变量未定义时的值,这个值自 ...

  7. pytorch下的lib库 源码阅读笔记(1)

    置顶:将pytorch clone到本地,查看initial commit,已经是麻雀虽小五脏俱全了,非常适合作为学习模板. 2017年12月7日01:24:15 2017-10-25 17:51 参 ...

  8. Python的Django

    1   第一部分目录详解 修改django的项目当中的url中的配置: from django.contrib import admin from django.conf.urls import ur ...

  9. Java构造器练习题

    仔细阅读下面的程序 public class Car { String name = "汽车"; public Car(String name) { this.name = nam ...

  10. 关于报错:There is already 'xxxController' bean method的解决方法

    报这个错的原因是因为你controller里的@RequestMapping中的路径有重复! 如: