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 ...
随机推荐
- solr6.0.0 + tomcat8 配置问题
中间件需求: apache-tomcat-8.0.32.tar.gz jdk-8u74-linux-x64.rpm solr-6.0.0.zip 0.安装java JDK rpm -ivh jdk-8 ...
- 显示intent和隐示intent有什么区别
显式Intent定义:对于明确指出了目标组件名称的Intent,我们称之为显式Intent. 隐式Intent定义:对于没有明确指出目标组件名称的Intent,则称之为隐式Intent. 说明:And ...
- 解决iis7 0x80070002 错误代码问题
环境:iis7 win7 64位 vs2012 出现的问题:发布代码iis出现0x80070002 错误代码 解决方案分三步: 1.添加通配符脚本映射 请选择:C:\Windows\Microsoft ...
- EMC起步:华为交换机拆解
[作者:byeyear Email:east3@163.com 转载请保留此行] 1. 静电抗扰 理想情况下,我们的系统是一个中空且密闭的金属盒子,根据电磁场理论,外界的任何静电源都不可能 ...
- HBase设计与开发性能优化(转)
本文主要是从HBase应用程序设计与开发的角度,总结几种常用的性能优化方法.有关HBase系统配置级别的优化,这里涉及的不多,这部分可以参考:淘宝Ken Wu同学的博客. 1. 表的设计 1.1 Pr ...
- SQL Server的高级知识
DataSet的内容介绍,两种单例模式(并发量的考虑),SQL高级中的case语句.连接操作.子查询.派生表 -------------------------------- 1.Case的用法 使用 ...
- npm命令ionic安装失败cordova安装失败解决方法
转载:http://bbs.phonegap100.com/thread-2622-1-1.html 镜像使用方法(三种办法任意一种都能解决问题,建议使用第三种,将配置写死,下次用的时候配置还在): ...
- RabbitMQ(四)
RabbitMQ 配置 一.RabbitMQ 配置修改方式 1.修改环境变量 2.修改配置文件(只介绍这个) 3.修改运行时参数和政策 locate rabbitmq vi /var/log/rabb ...
- Linux学习笔记(二)
1.tzselect无法是使用 vim /usr/bin/tzselect 将 ${TZDIR=pwd}改为${TZDIR=/usr/share/zoneinfo} 2.sudo apt-get in ...
- fzu1342
http://acm.fzu.edu.cn/problem.php?pid=1342 dp[i][j] , i位尾巴为j的概率 const int maxn = 1008 ; double dp[m ...