当前位置:网站首页>【LeetCode】两数之和II
【LeetCode】两数之和II
2022-06-23 03:32:00 【LawsonAbs】
1 题目
2 思想
翻转链表后再做计算,然后再翻转链表。本题要求对链表的翻转操作比较熟练。
3 代码
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
l1 = self.reverseLinkList(l1)
l2 = self.reverseLinkList(l2)
head_l1 = l1
# 执行加法
flag = 0
head = None
while(l1 and l2):
flag ,l1.val = divmod(l1.val + l2.val + flag,10)
head = l1
l1 = l1.next
l2 = l2.next
if l2:
l1 = l2
head.next = l1
while(l1):
flag,l1.val = divmod(l1.val+flag,10)
head = l1
l1 = l1.next
# 如果最后还有进位
if flag :
head.next = ListNode(flag,None)
return self.reverseLinkList(head_l1)
# 翻转链表
def reverseLinkList(self,head):
# 就地翻转
nxt = head.next
pre = None
while(head and nxt):
tmp = nxt.next
head.next = pre # 修改
nxt.next = head
pre = head
head = nxt
nxt = tmp
return head # 返回翻转后的链表
边栏推荐
- Wwdc21 - App store server API practice summary
- [Alibaba middleware technology series] "Nacos technology" service registration and discovery related principle analysis
- Static code block, code block, constructor execution order
- Heavyweight review: strategies for reliable fMRI measurements
- What is the difference between comparator and comparable?
- Learning record -- superficial understanding of unity decoupling
- What should be prepared to develop the company's official website?
- How to save the model obtained from sklearn training? Just read this one
- Why don't I suggest you use "! = null" to judge empty space?
- JS to realize character case switching
猜你喜欢

Svn local computer storage configuration

Analysis on the development of China's graphene industry chain in 2021: with the support of energy conservation and environmental protection policies, the scale of graphene industry will continue to e

【贪心】leetcode991. Broken Calculator
![Analysis of the number of urban residents covered by basic medical insurance, their treatment and medical treatment in other places in China in 2021 [figure]](/img/81/4d3cb059f700dd9243645e64023be7.jpg)
Analysis of the number of urban residents covered by basic medical insurance, their treatment and medical treatment in other places in China in 2021 [figure]

Analysis on the development of China's satellite navigation industry chain in 2021: satellite navigation is fully integrated into production and life, and the satellite navigation industry is also boo

LRU cache
![Analysis on development history, industrial chain, output and enterprise layout of medical polypropylene in China in 2020 [figure]](/img/28/ebfc25ec288627706e15a07e6bdb77.jpg)
Analysis on development history, industrial chain, output and enterprise layout of medical polypropylene in China in 2020 [figure]

Encryption related to returnee of national market supervision public service platform
![Analysis on the development prospect of China's brain computer interface industry in 2021: wide application prospect, sustained and rapid growth of market scale [figure]](/img/84/192d152ceb760264b6b555b321f129.jpg)
Analysis on the development prospect of China's brain computer interface industry in 2021: wide application prospect, sustained and rapid growth of market scale [figure]

1058 multiple choice questions (20 points)
随机推荐
How to get started with apiccloud app and multi terminal development of applet based on zero Foundation
YouTube security scenarios
This point (II)
Free upgrade of 2-core 2GB for old generation 1-core 2GB machines below standard S5 and SA2
To implement a task scheduling system, it is enough to read this article
How to make distribution box label
Static code block, code block, constructor execution order
Get method of fetch request and data of formdata type submitted by post
Analysis of China's integrated circuit industry chain in 2021: huge downstream market demand [figure]
An implementation of universal interface caching Middleware
Ultra detailed Apache and PHP installation tutorial windows (2022.1)
Easygbs solution to video playback failure caused by tcp/udp configuration problems
How to generate code-11 barcode in batch through TXT file
[advanced Android] entrusted by kotlin
Jmeter- (V) simulated user concurrent login for interface test
One of the touchdesigner uses - Download and install
Decentralized networks are not decentralized
新版kali切换最高账户
Analysis on the development of China's graphene industry chain in 2021: with the support of energy conservation and environmental protection policies, the scale of graphene industry will continue to e
Frequent actions to expand the scale of new energy industry Guangzhou plans to invest 1.4 billion in photovoltaic power generation projects