Leetcode2:Add Two Numbers@Python
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
#-*-coding:utf-8-*- #Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
tens = 0 # l1.val+l2.val所得和的十位数,初始化0
units = 0 # l1.val+l2.val所得和的个位数,初始化0
l_origin = ListNode(0) # 表示一个链表的头结点
l = l_origin
while(l1 or l2 or tens!=0):
val = tens
if l1:
val = val + l1.val
l1 = l1.next
if l2:
val = val + l2.val
l2 = l2.next
units = val % 10
tens = val / 10
node = ListNode(units)
l.next = node
l = l.next
return l_origin.next # 返回所求列表
在Leetcode上提交时直接提交Solution类,结点定义不用提交。
在做这题时用到带有头结点的链表,比不带头结点的链表使用时方便的多。
Leetcode2:Add Two Numbers@Python的更多相关文章
- Leetcode-2 Add Two Numbers
#2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...
- leetcode2:Add Two Numbers
Add Two Numbers Total Accepted: 55216 Total Submissions: 249950My Submissions You are given two link ...
- 2. Add Two Numbers——Python
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- Leetcode 解题 Add Two Numbers Python
原题: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- leetcode-2 Add Two Numbers 计算两个对应的列表和问题
1.问题描写叙述: You are given two linked lists representing two non-negativenumbers. The digits are sto ...
- LeetCode-2. Add Two Numbers(链表实现数字相加)
1.题目描述 You are given two non-empty linked lists representing two non-negative integers. The digits a ...
- leetcode add two numbers python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- Leetcode2.Add Two Numbers两数相加
给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...
- LeetCode 2. 两数相加(Add Two Numbers)
2. 两数相加 2. Add Two Numbers 题目描述 You are given two non-empty linked lists representing two non-negati ...
随机推荐
- unity 内存中切割图片
一般的说我们切割图片是将一张图片资源切割成更小的图片资源,也就是说在资源上就进行了切割,比如ugui上的切割方法. 如果我们有一些情况比如做拼图,可能让玩家自己选择自己的生活照作为拼图的原图. 那么我 ...
- 理解jquery的$.extend()、$.fn和$.fn.extend()
jQuery为开发插件提拱了两个方法,分别是: jQuery.fn.extend(); jQuery.extend(); jQuery.fn jQuery.fn = jQuery.prototype ...
- virtio-blk简介[转]
声明: 本博客欢迎转发,但请保留原作者信息!新浪微博:@孔令贤HW: 博客地址:http://lingxiankong.github.io/内容系本人学习.研究和总结,如有雷同,实属荣幸! virti ...
- angular 实现tab切换(循环输出tab标题及tab下属内容,非direct,非include)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...
- 1、SQL可搜索可排序可分页存储过程, 2、范围内的随机时间 适用于sql 2008以上
-- ============================================= -- Author: 蜘蛛王 -- Create date: 2015-10-29 -- Descri ...
- MySql开启慢查询报错:Could not open /var/log/slow_query.log for logging (error 13).
Turning logging off for the whole duration of the MySQL server process. File '/var/log/slow_query.lo ...
- 【学】AngularJS日记(3)- $apply(), run()方法
$scope.$apply()方法可以强制$apply()里运行的函数所改变的model里的数据直接反应到view里,因为在angular的环境中,有时会用到原生js或者jquery的时候,这些行为有 ...
- Node.js的process.nextTick(callback)理解
Node.js是单线程的,基于事件循环,非阻塞 IO的.事件循环中使用一个事件队列,在每个时间点上,系统只会处理一个事件,即使电脑有多个CPU核心,也无法同时并行的处理多个事件.因此,node.js适 ...
- Java内存区域
1.运行时数据区域 java虚拟机在执行java程序的过程中会将它管理的内存区域分为若干个不同的数据区域.这些区域有各自的服务对象,创建以及销毁时间,有的内存区域随着虚拟机的启动和关闭而创建和销毁,有 ...
- EventBus--介绍
注意: 1,post()方法里面的类型和onEvent()中的类型要一致., 2,订阅者对象中 必须有 onEvent 的 public 方法 ---public void onEvent(O ...