Leetcode_两数相加_python
小编从今天起要开始分享一些Leetcode代码,通过好好练习编程能力,争取以后找一份好工作。
题目:两数相加

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
#l1.val表示当前链表的值,l1.next表示链表指针指向下一个节点
#创建头节点
self.head=ListNode(0)
p=self.head
more=0
more1=0
more2=0
while(l1!=None and l2!=None):
value=l1.val+l2.val+more data=list(str(value)) if(len(data)==1):
node=ListNode(int(data[0]))
print(int(data[0]),more)
more=0
else:
node=ListNode(int(data[1]))
print(int(data[1]),more)
more=int(data[0]) p.next=node
p=p.next
l1=l1.next
l2=l2.next if(l1!=None and l2==None):
more1=more
if(l2!=None and l1==None):
more2=more while(l1!=None):
value=l1.val+more1
data=list(str(value))
#print(data)
if(len(data)==1):
node=ListNode(int(data[0]))
print(int(data[0]),more1)
more1=0
else:
node=ListNode(int(data[1]))
print(int(data[1]),more1)
more1=int(data[0])
#print(more1)
p.next=node
p=p.next
l1=l1.next
more=0
while(l2!=None):
value=l2.val+more2
data=list(str(value))
#print(data)
if(len(data)==1):
node=ListNode(int(data[0]))
more2=0
else:
node=ListNode(int(data[1]))
more2=int(data[0])
#print(more)
p.next=node
p=p.next
l2=l2.next
more=0
if(more!=0):
node=ListNode(more)
p.next=node
if(more1!=0):
node=ListNode(more1)
p.next=node
if(more2!=0):
node=ListNode(more2)
p.next=node
p=self.head
p.val=p.next.val
p.next=p.next.next
return p
Leetcode_两数相加_python的更多相关文章
- leetcode_两数相加
给你两个 非空 的链表,表示两个非负的整数.它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字. 请你将两个数相加,并以相同形式返回一个表示和的链表. 你可以假设除了数字 0 ...
- [CareerCup] 18.1 Add Two Numbers 两数相加
18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...
- ✡ leetcode 167. Two Sum II - Input array is sorted 求两数相加等于一个数的位置 --------- java
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- [Swift]LeetCode2. 两数相加 | Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- [Swift]LeetCode445. 两数相加 II | Add Two Numbers II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
- day2——两数相加
// 小白一名,0算法基础,艰难尝试算法题中,若您发现本文中错误, 或有其他见解,往不吝赐教,感激不尽,拜谢. 领扣 第2题 今日算法题干//给定两个非空链表来表示两个非负整数.位数按照逆序方式存储, ...
- leetcode 刷题(2)--- 两数相加
给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...
- Leetcode(二)两数相加
两数相加 题目描述 给出两个非空的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链 ...
- 【LeetCode】两数相加
题目描述 给出两个非空的链表用来表示两个非负的整数.其中,它们各自的位数是按照逆序的方式存储的,并且它们的每个节点只能存储一位数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和. ...
随机推荐
- 安装Visual Studio 语言包时出现windows 程序兼容模式已打开.请将其关闭
打开 cmd.exe 输入 安装包路径 /Uninstall例如:D:\vs_lang_cn.exe /Uninstall (中间有个空格,Uninstall前是个左斜杠)回车 后 安装包会运行.点击 ...
- 蓝桥杯省赛 牌型种数java
小明被劫持到X赌城,被迫与其他3人玩牌. 一副扑克牌(去掉大小王牌,共52张),均匀发给4个人,每个人13张.这时,小明脑子里突然冒出一个问题:如果不考虑花色,只考虑点数,也不考虑自己得到的牌的先后顺 ...
- 搭建一个microblaze的最小系统
搭建一个microblaze的最小系统 首先例化一个microblaze核 对microblaze核进行配置 勾选: 这个是使能外部存储BRAM作为程序运行缓冲区 第二个是需要勾选 这个是使能AXI数 ...
- LAB1 partI
序言 1. master.go 可以正常调用Distributed() 和 Sequential(). Sequential 顺序执行主要用于调试. 2. master : 创建一个 RPC serv ...
- 在linux中输出每个group的用户成员
先提供使用文件一步一步获取相关信息: 1. 获取所有的用户: awk -F: '{print $1 > "1.txt"}' /etc/passwd 2. 获取每个用户, 及其 ...
- rxjava&retrofit请求直接返回string
1.添加gradle依赖: compile com.squareup.retrofit2:converter-scalars:2.0.0' 2.更换转换器 mRetrofit = Retrofit.B ...
- VMWare给macos虚拟机扩容方法
一开始在VMWareWorkStation上创建macos虚拟机时,我考虑到物理硬盘大小有限,只分配了34G,随着不断的使用,虚拟机消耗的虚拟磁盘逐渐增长,因磁盘空间不足而导致无法在虚拟机中使用xco ...
- [STM32F103]PWM输入捕获配置
l 初始化定时器和通道对应IO的时钟. l 初始化IO口,模式为输入: GPIO_Init(); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0 ...
- 音乐出身的妹纸,零基础学习JAVA靠谱么
问:表示音乐出身的妹纸一枚 某一天突然觉得身边认识的是一群程序员 突然想 要不要也去试试... 众好友都觉得我该去做个老师,可是我怕我会误人子弟,祸害祖国下一代..... 要不要 要不要 学Ja ...
- bs4源码
Beautiful源码: """Beautiful Soup Elixir and Tonic "The Screen-Scraper's Friend&quo ...