题目

You are given two linked lists representing two non-negative numbers. 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.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

代码:oj测试通过 Runtime: 171 ms

 # Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @return a ListNode
def addTwoNumbers(self, l1, l2):
if l1 is None:
return l2
if l2 is None:
return l1 dummyhead = ListNode(0)
p = ListNode(0)
dummyhead.next = p jinwei = 0
while l1 is not None and l2 is not None:
curr_total = l1.val + l2.val + jinwei
l1 = l1.next
l2 = l2.next
curr_digit = curr_total % 10
jinwei = curr_total / 10
curr_node = ListNode(curr_digit)
p.next = curr_node
p = p.next if l1 is not None:
while l1 is not None:
curr_total = l1.val + jinwei
l1 = l1.next
curr_digit = curr_total % 10
jinwei = curr_total / 10
curr_node = ListNode(curr_digit)
p.next = curr_node
p = p.next
if l2 is not None:
while l2 is not None:
curr_total = l2.val + jinwei
l2 = l2.next
curr_digit = curr_total % 10
jinwei = curr_total / 10
curr_node = ListNode(curr_digit)
p.next = curr_node
p = p.next if jinwei == 1:
curr_node = ListNode(1)
p.next = curr_node return dummyhead.next.next

思路

就是加法运算 注意两条链表上所有值计算过后 是否有进位;如果有进位 需要再处理一下。

leetcode 【 Add Two Numbers 】 python 实现的更多相关文章

  1. leetcode add two numbers python

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...

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

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

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

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

  4. LeetCode Add Two Numbers II

    原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...

  5. LeetCode: Add Two Numbers 解题报告

    Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...

  6. Leetcode:Add Two Numbers分析和实现

    Add Two Numbers这个问题的意思是,提供两条链表,每条链表表示一个十进制整数,其每一位对应链表的一个结点.比如345表示为链表5->4->3.而我们需要做的就是将两条链表代表的 ...

  7. [LeetCode] Add Two Numbers题解

    Add Two Numbers: You are given two non-empty linked lists representing two non-negative integers. Th ...

  8. Leetcode 解题 Add Two Numbers Python

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

  9. Leetcode2:Add Two Numbers@Python

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

  10. [Leetcode] Add two numbers 两数之和

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

随机推荐

  1. C# 只运行一个实例 ShowWindowAsync 窗体隐藏时失效 解决方案

    如果窗体已经隐藏,那么利用instance.MainWindowHandle得到的句柄为空,继而ShowWindowAsync 操作失败 不过我们可以使用FindWindow来查找到指定窗体的句柄 只 ...

  2. 彻底解决Android 应用方法数不能超过65K的问题

    作为一名Android开发者,相信你对Android方法数不能超过65K的限制应该有所耳闻,随着应用程序功能不断的丰富,总有一天你会遇到一个异常: Conversion to Dalvik forma ...

  3. Mybatis-Spring整合Spring

    因为 MyBatis 用 SqlSessionFactory 来创建 SqlSession ,SqlSessionFactoryBuilder 创建 SqlSessionFactory ,而在 Myb ...

  4. hbase查询语法

    1.scan '表名',{FILTER=>"PrefixFilter('rowkey值')"} scan 'useractions',{FILTER=>"Pr ...

  5. linux 命令——52 ifconfig(转)

    许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口配置信息并对此进行修改.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config).通常需 ...

  6. CSS样式中visited,hover,active , focus这四个分别表示什么意思?

    CSS伪类用于向某些选择器添加特殊的效果.CSS又名层叠样式表,所谓层叠,就是后面的样式会覆盖前面的样式,所以在样式表中,各样式排列的顺序很有讲究. :link 与 :visited 在样式文件中的顺 ...

  7. 【BZOJ1965】[AHOI2005] SHUFFLE 洗牌(数学题)

    点此看题面 大致题意: 有一叠扑克牌编号为\(1\sim n\)(\(n\)为偶数),每次洗牌将扑克牌平均分成上下两叠,取下面一叠的第一张作为新的一叠的第一张,然后取上面一叠的第一张作为新的一叠的第二 ...

  8. Being a Good Boy in Spring Festival(博弈)

    Being a Good Boy in Spring Festival Problem Description一年在外 父母时刻牵挂春节回家 你能做几天好孩子吗寒假里尝试做做下面的事情吧 陪妈妈逛一次 ...

  9. 2018.5.28 Oracle数据库补充

    select * from (select rownum rn,e2.* from (select e1.* from emp e1)e2 where rownum<=10)e3 where e ...

  10. CentOS启动时自动加载内核模块

    要想在CentOS中自动加载内核模块,需要在/etc/sysconfig/modules/目录中增加一个脚本,在此脚本中加载所需的模块. 下面是我所用的一个名为8021q.modules的脚本,用来在 ...