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】两数相加
题目描述 给出两个非空的链表用来表示两个非负的整数.其中,它们各自的位数是按照逆序的方式存储的,并且它们的每个节点只能存储一位数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和. ...
随机推荐
- Elasticsearch(单节点)
1 Elasticsearch搭建 1.1 通过Wget下载ElasticSearch安装包wget https://artifacts.elastic.co/downloads/elasticsea ...
- C goto
http://c.biancheng.net/view/266.html 当程序遇到 goto 后, 会无条件跳转到标签后出,然后程序按照顺序执行 例子: #include <stdio.h&g ...
- puzz: 图片和表单上传的不一致问题
1. 方向1 用户提交表单, 图片和表单同步上传.(由同一服务器处理, 服务器压力大. 没有分离) 2. 方向2 图片和表单分开上传. 如图片访问ftp,表单提交后台(图片和后台分离) 2 ...
- 如何使用linux+xvfb+python+rfs+firefox+jenkins实现UI自动化
首先说一下背景,在项目中使用windows执行机进行rfs脚本运行时,遇到两个问题: 1.Jenkins的多个slave节点和master的连接通过windows插件去建立,如果出现slave断连,无 ...
- 微服务与Spring Cloud资料
Microservices Using Spring Boot and Spring Cloud 微服务注册中心 Eureka 架构深入解读 50+ 顶级开源 Kubernetes 工具列表 Apol ...
- catkin-make: command not found 错误解决
参考网址:https://answers.ros.org/question/212492/catkin_make-command-not-found/ zc@ubuntu:~ $ source /op ...
- python笔记(三)---文件读写、修改文件内容、处理json、函数
文件读写(一) #r 只读,打开文件不存在的话,会报错 #w 只写,会清空原来文件的内容 #a 追加写,不会请求,打开的文件不存在的话,也会帮你新建的一个文件 print(f.read()) #获取到 ...
- Go语言 并发编程
Go语言 并发编程 作者:Eric 微信:loveoracle11g 1.创建goroutine // 并行 是两个队列同时使用两台咖啡机 // 并发 是两个队列交替使用一台咖啡机 package m ...
- IC5141安装备忘
X Error of failed request: BadName (named color or font does not exist) Major opcode of failed re ...
- C#求一组数的众数
private int GetModeNum(List<int> listValue) { List<int> listName = new List<int>() ...