题目

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. cocos2d-x 配置教程

    下载cocos2d-x并解压(解压的路径不要带空格,不然以后用cygwin编译工程的时候会出现错误),我的解压到了D:\cocos2d-2.1rc0-x-2.1.2(我的版本是cocos2d-2.1r ...

  2. javascript HTML静态页面传值的四种方法

    一:JavaScript静态页面值传递之URL篇能过URL进行传值.把要传递的信息接在URL上.Post.htm 代码如下: <input type="text" name= ...

  3. php使用GD库实现图片水印和缩略图——给图片添加图片水印

    今天呢,就来学习一下在php中使用PD库来实现对图片水印的文字水印方法,不需要PS哦! 首先,准备素材 (1)准备一张图片 (2)准备一张水印(最好是透明的,即背景是白色底) (3)准备一中字体(在电 ...

  4. 在github中的READEME中添加图片或者动图

    在github中reademe中添加动图或者图片 将你需要展示的图片放在这个项目中的某个文件夹中,然后再reademe中这样引入 ![maze](https://github.com/GainLoss ...

  5. libav(ffmpeg)简明教程(1)

    突然发现又有好久没有写技术blog了,主要原因是最近时间都用来研究libav去了(因为api极类似ffmpeg,虽然出自同一份代码的另外一个分支,因项目选用libav,故下文均用libav代替),其实 ...

  6. postman传递参数的问题

    postman是一款通过post或者get发送请求测试代码的工具 如果是类的话,就选择JSON格式,如果是一个字段的方法,就直接写入方法值就好了比如 public PageResult<Info ...

  7. 【UVA10652】Board Wrapping(求凸包面积)

    点此看题面 大致题意: 告诉你若干个矩形的重心坐标.长.宽和相对\(y\)轴的偏转角度,求矩形面积和与能围住这些矩形的最小凸包面积之比. 矩形面积和 这应该是比较好求的吧. 已经给了你长和宽,直接乘起 ...

  8. Load事件中控件Focus()无效解决办法

    原因:Load窗体时,窗体未显示 解决:1.Focus()之前添加this.Show(); 2.在Shown事件中添加Focus()

  9. 1412: [ZJOI2009]狼和羊的故事

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4017  Solved: 2037[Submit][Status][Discuss] Descript ...

  10. 使用paramiko报错:CryptographyDeprecationWarning: Support for unsafe construction of public numbers from encoded data will be removed in a future version. Please use EllipticCurvePublicKey.from_encoded_poi

    1.paramiko不支持cryptography-2.6.1 pip3 uninstall cryptography==2.6.1 2.paramiko 支持cryptography-2.4.2 p ...