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 ...
随机推荐
- Windows Redis使用
一.Redis 的安装 1.Redis 下载 Windows 版本下载:https://github.com/dmajkic/redis/downloads 2.解压到 C:\redis-2.4.5- ...
- [转]MySQL Connector/C++(一)
http://www.cnblogs.com/dvwei/archive/2013/04/18/3029464.html#undefined#undefined MySQL Connector/C++ ...
- GDB详解
1 简介 2 生成调试信息 3 启动GDB 的方法 4 程序运行上下文 4.1 程序运行参数 4.2 工作目录 4.3 程序的输入输出 5 设置断点 5.1 简单断点 5.2 多文件设置断点 5.3 ...
- WeX5 - AJAX跨域调用相关知识-CORS和JSONP
http://docs.wex5.com/ajax-cross-domain/ 1.什么是跨域 跨域问题产生的原因,是由于浏览器的安全机制,JS只能访问与所在页面同一个域(相同协议.域名.端口)的内容 ...
- tcpdump抓SQL[转]
转自:http://www.cnblogs.com/LMySQL/p/5060604.html 前言:假设如果有个服务器几十个链接突然达到上千个链接,show processlist,general_ ...
- Markdown 软件
<<github.css>> Markdown 软件 :Typora / Haroopad / Markdownpad2 Markdownpad2软件注册及下载地址 邮箱地址: ...
- c++中的宏 #define _CLASSDEF(name) class name
#include <iostream> using namespace std; #define _CLASSDEF(name) class name; \ typedef name * ...
- Java JIT(Just-In-Time) Compilation
http://www.oracle.com/technetwork/articles/java/architect-evans-pt1-2266278.html
- Git使用培训
1.VS“扩展和更新”菜单,NuGet 2.通过“扩展和更新”,联机搜索git,安装“Git Extensions”和“Git Source Control Provider” 3.通过Git命令配置 ...
- STM32 USB转串口驱动 Virtual COM Port Driver(V1.3.1)
将stm32的USB口接到PC端后,PC端会要求安装一个STM32 USB Virtual COM Port Driver,然后就可以用串口调试助手对其操作了