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 ...
随机推荐
- oracle数据库从入门到精通之三
综合案例ddl&dml有一个商品数据库1.数据表的创建 ddl先编写数据库脚本--删除数据表drop table purcase purge;drop table product pur ...
- Tencent://Message/协议的实现原理
腾讯官方通过 Tencent://Message/协议可以让QQ用户显示QQ/TM的在线状态发布在互联网上:并且点击 XXX ,不用加好友也可以聊天 官方链接: http://is.qq.com/w ...
- 判断Activity是否正在退出 isFinishing()
boolean android.app.Activity.isFinishing() Added in API level 1 Check to see whether this ...
- Swift 简介
1.swift支持所有C和Obeject-c的基本类型,支持面向过程和面向对象的编程机制. 2.swift提供了2种功能强劲的集合类型:数组和字典 3.元祖 4.可选类型 5.swift 是一种类型安 ...
- Hibernate5.2之反向工程
Hibernate5.2之反向工程 一.描述 可能很多人在使用Hibernate进行项目开发 ...
- animated js动画示例
function fabtn(a){ $(a).find('i').addClass('animated wobble'); setTimeout(function(){ $(a).find('i') ...
- emacs配置eslint 语法检查.找不到node解决
使用emacs配置eslint 当调用语法检查时报错 Suspicious state from syntax checker javascript-eslint: Checker javascrip ...
- OpenCV配置经历简述
关于OpenCV的配置过程在这里做一简述和记录. 配置的是OpenCV2.2.0,环境为VS2010. 首先在OpenCV官网(http://opencv.org/downloads.html)下载了 ...
- 阿里云CentOS6上配置iptables
参考:http://blog.abv.cn/?p=50 阿里云CentOS6默认没有启动iptables 1.检查iptables状态 [root@iZ94jj63a3sZ ~]# service i ...
- Oauth2.0
注:Access token 为第三方平台的token,在用户授权时使用Refresh Token为用户的token get/post参数时,一般采用字典排序