当前位置:网站首页>【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 # 返回翻转后的链表
边栏推荐
- Evolution of cloud firewall products
- RTOS system selection for charging point software design
- Using promise to process asynchronous operations
- Why don't I suggest you use "! = null" to judge empty space?
- Preliminary sequencing problem
- 【二叉树】993. Cousins in Binary Tree
- Analysis of China's integrated circuit industry chain in 2021: huge downstream market demand [figure]
- Account MFA usage scheme
- MIT 6. S081/fall 2020 build risc-v and xv6 development and debugging environment
- PHP composer yii2 installation
猜你喜欢

Encryption related to returnee of national market supervision public service platform

LRU cache

Detailed discussion on modular architecture design of MCU firmware

R tree of search tree

新版kali切换最高账户

One of the touchdesigner uses - Download and install
![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]

mysql 数据恢复 (.ibdata1, bin log)

Analysis on the development of duty-free industry in Hainan Province in 2021: the implementation of the new policy makes the duty-free market in Hainan more "prosperous" [figure]

1058 multiple choice questions (20 points)
随机推荐
Concept and function of ES6 symbol
One of the touchdesigner uses - Download and install
What is the difference between comparator and comparable?
CentOS install redis
JS Part 4
Configuring multi cluster management using kubectl
JS to realize character case switching
Simple analysis of easygbs compatible with old version HLS streaming address method [with code]
How to make special labels for books
Static code block, code block, constructor execution order
YouTube security scenarios
Official announcement! The Hong Kong Zhuhai Macao Bridge is finally here!
Email authentication bypass
Enterprise official website applet building tutorial
Even if you don't learn gradle, these common development operations are worth mastering
How to batch generate jan13 barcode
Weekly Postgres world news 2022w03
Quickly understand the development status of secondary nodes of industrial Internet identity analysis
Simply use the pagoda to build WordPress
Analysis on the development of duty-free industry in Hainan Province in 2021: the implementation of the new policy makes the duty-free market in Hainan more "prosperous" [figure]