• 题目描述:输入两个非空单链表,链表的每个结点的值是一个1位数整数,两个链表都是一个大整数每一位的逆序排序,求这两个链表代表的整数的和的链表值;

  • 思路:

  1. 分别遍历两个链表,转化成相应的整数,求和后把结果每一位转化成单链表即可;
class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
a = b = 0
carry = 0
while l1:
a += l1.val * 10 ** carry
carry += 1
l1 = l1.next
carry = 0
while l2:
b += l2.val * 10 ** carry
carry += 1
l2 = l2.next
ret = a + b
h = m = ListNode(0)
if not ret:
return h
while ret:
m.next = ListNode(ret % 10)
ret /= 10
m = m.next
return h.next

Python 解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(2) || Add Two Numbers && Longest Substring Without Repeating Characters

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

  3. LeetCode:1. Add Two Numbers

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

  4. LeetCode 面试:Add Two Numbers

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

  5. LeetCode #002# Add Two Numbers(js描述)

    索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...

  6. LeetCode 2. add two numbers && 单链表

    add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...

  7. [Leetcode Week15] Add Two Numbers

    Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...

  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 ...

  10. LeetCode之Add Two Numbers

    Add Two Numbers 方法一: 考虑到有进位的问题,首先想到的思路是: 先分位求总和得到 totalsum,然后再将totalsum按位拆分转成链表: ListNode* addTwoNum ...

随机推荐

  1. number-progression-network

    T1给定一个 $n$ 位的数字串,要求修改若干位,使得至少包含 $k$ 个相同的数位,最小化代价. Sol 考虑枚举那种数字作为答案,选代价前 $k$ 小的修改成目标数字. 有一部分的数字是必须修改的 ...

  2. 【线性代数】6-6:相似矩阵(Similar Matrices)

    title: [线性代数]6-6:相似矩阵(Similar Matrices) categories: Mathematic Linear Algebra keywords: Similar Matr ...

  3. 关于openstack 网络相关的文章收集

    journalctl工具基础介绍(你需要这个的.) https://blog.51cto.com/13598893/2072212 新版devstack使用systemd的方式来管理OpenStack ...

  4. 使用apktool工具遇到 could not decode arsc file 的解决办法

    I: Using Apktool -Beta9 on xx.apk I: Loading resource table... Exception in thread "main" ...

  5. About Xi’an

    Introduction Ancient Capital It is the birthplace of the Chinese Nation, is one of the four ancient ...

  6. LC 592. Fraction Addition and Subtraction

    Given a string representing an expression of fraction addition and subtraction, you need to return t ...

  7. JAVA踩坑录

    以前踩了很多坑,大多忘了.现在踩了坑,想起了一定记下来. 1. 字符串分割,这种工具类,首次使用一定要先看一眼,不然跳坑 commons-lang StringUtils.split分割时会去掉空串: ...

  8. 【NetDevops】网络自动化运维--1获取用户基本信息

     版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.  之前博客的云主机到期了没续费,被删啦最重要的是没有备份!此处省略几个字.....      ...

  9. [Java读书笔记] Effective Java(Third Edition) 第 6 章 枚举和注解

    Java支持两种引用类型的特殊用途的系列:一种称为枚举类型(enum type)的类和一种称为注解类型(annotation type)的接口. 第34条:用enum代替int常量 枚举是其合法值由一 ...

  10. Node.js启动服务报错SyntaxError: Unexpected token import

    启动服务报错如下: Last login: Wed Nov :: on ttys000 localhost:~ sipeng$ cd /Users/sipeng/Desktop/彭思/2017年学习/ ...