当前位置:网站首页>【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 # 返回翻转后的链表
边栏推荐
- 2022-01-22: Li Kou 411, the abbreviation of the shortest exclusive word. Give a string number
- Goframe framework (RK boot): realize distributed log tracing
- Hierarchical attention graph convolution network for interpretable recommendation based on knowledge graph
- How to get started with apiccloud app and multi terminal development of applet based on zero Foundation
- JSON. Function of the stringify() optional parameter
- Wwdc21 - App store server API practice summary
- centos7 安装 MySQL 及配置 innodb_ruby
- "Tencent conference": how to operate and maintain efficiently in the face of exponential growth of business?
- How to save the model obtained from sklearn training? Just read this one
- 【二分】leetcode1011. Capacity To Ship Packages Within D Days
猜你喜欢
![[quick view] Analysis on the development status and future development trend of the global and Chinese diamond cultivation industry in 2021 [figure]](/img/f1/972a760459a6d599b5681aa634df09.jpg)
[quick view] Analysis on the development status and future development trend of the global and Chinese diamond cultivation industry in 2021 [figure]

Fetch request details

Hierarchical attention graph convolution network for interpretable recommendation based on knowledge graph

【贪心】leetcode991. Broken Calculator

Encryption related to returnee of national market supervision public service platform

centos7 安装 MySQL 及配置 innodb_ruby
![Analysis on the development status of China's watch industry in 2021: a large number of electric watches are imported [figure]](/img/ca/672bfe49c8123da8679b2abeb43a2e.jpg)
Analysis on the development status of China's watch industry in 2021: a large number of electric watches are imported [figure]
![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]

1-1VMware介绍
![Analysis on demand and market scale of China's steamed stuffed bun industry in 2020 [figure]](/img/4b/dd272f98b89a157180bf68570d2763.jpg)
Analysis on demand and market scale of China's steamed stuffed bun industry in 2020 [figure]
随机推荐
Eight models of data analysis: detailed explanation of RFM model
Nezha panel modifies logo, small icon and other information
Application of map function in JS
Flowable refactoring process editor to obtain user information
Heavyweight review: strategies for reliable fMRI measurements
新版kali切换最高账户
Engineer culture: should the company buy genuine software
What are the advantages and difficulties of introducing AI into ISP Technology
"Tianzhou II" successfully docked! Three minutes to understand the shocking black technology on "Tianzhou II"! Headlines
Centos7.6 installing mysqlclient error reporting solution
Ultra detailed Apache and PHP installation tutorial windows (2022.1)
SwiftUI 组件大全之使用 ScrollView 和 GeometryReader 创建动画 3D卡片 滚动效果
Network security memorabilia - Summary of vulnerability exploitation events in 2021
CentOS install redis
Build information query applet by using micro build
How to make distribution box label
Customization of openfeign
The MIUI 13 development version of Xiaomi mobile phone blocks the chrome application and cannot be opened after installation
How does easyplayer embed a video snapshot into a demo?
Aiot application innovation competition - Smart street lamp (developed with keil)