题目

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. Spring Boot 的配置文件application.properties

    Spring Boot 中的application.properties 是一个全局的配置文件,放在src/main/resources 目录下或者类路径的/config下. 作为全局配置文件的app ...

  2. Js面向对象之观察者模式

    //模拟一个目标可能拥有的一些列依赖 function ObserverList() { this.observerList = []; }; //添加一个观察者 ObserverList.proto ...

  3. 企业常用的站内收索、QQ群、在线客服

    <div class="toplinks">            <form target="_blank">             ...

  4. c++ STL deque容器成员函数

    deque是双向队列,即可以在头部插入删除,也可以在尾部插入删除.内部并不连续,这一点和vector并不一样.可能第1个元素和第2个元素的地址是不连在一起的.在使用时用it迭代器会安全一点. 这是c+ ...

  5. ASP.net Session阻塞、Session锁、MVC Action请求阻塞问题

    会话Session Session用于服务器端状态管理,使用Session之后,每个客户端都可以将实际的数据保存在服务器上,对于每个客户端的数据,将会生成一个对应的唯一的key(保存在客户端).客户端 ...

  6. linux 命令——28 tar

    通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候tar命令就是是必不可少的一个功能强大的工具.linux中最流行的tar是麻雀虽小,五脏俱全,功能强大.tar命令可以为linux的 ...

  7. C语言标准库之setjmp

    协程的介绍 协程(coroutine),意思就是“协作的例程”(co-operative routines),最早由Melvin Conway在1963年提出并实现.跟主流程序语言中的线程不一样,线程 ...

  8. x5webview 微信H5支付

    mWebView.setWebViewClient(new WebViewClient() { // @Override // public boolean shouldOverrideUrlLoad ...

  9. 如何从SqlDataReader中获取DataRow

    1.前言使用SqlDataReader较之SqlDataAdapter速度上快,因为读取记录是一行一行的来,但是往往也感觉那么蹩脚.例如经常用到的如何从SqlDataReader中获取DataRow呢 ...

  10. wepy框架构建小程序(1)

    wepy框架构建小程序(1) 基本操作: # 安装脚手架工具 npm install wepy-cli -g # 创建一个新的项目 npm init standard myproject # 进入新项 ...