当前位置:网站首页>[leetcode] sum of two numbers II
[leetcode] sum of two numbers II
2022-06-23 03:38:00 【LawsonAbs】
1 subject
2 thought
Turn over the linked list before calculating , Then flip the linked list . This question requires that you be familiar with the operation of flipping the linked list .
3 Code
# 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
# Perform addition
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 there's a carry at the end
if flag :
head.next = ListNode(flag,None)
return self.reverseLinkList(head_l1)
# Flip list
def reverseLinkList(self,head):
# Flip in place
nxt = head.next
pre = None
while(head and nxt):
tmp = nxt.next
head.next = pre # modify
nxt.next = head
pre = head
head = nxt
nxt = tmp
return head # Returns the flipped linked list
边栏推荐
- Build information query applet by using micro build
- ABCD identifier of SAP mm initial cycle count
- Mybatties plus batch warehousing
- mysql 数据恢复 (.ibdata1, bin log)
- What about the high cost of storage system? The original computer room can save so much money!
- Static code block, code block, constructor execution order
- What is the potential of dmail based on Web3.0? First round financing of $10 million?
- What should be prepared to develop the company's official website?
- Firewall and IP security policy configuration
- 【LeetCode】两数之和II
猜你喜欢

Svn local computer storage configuration
![[OWT] OWT client native P2P E2E test vs2017 construction 4: Construction and link of third-party databases p2pmfc exe](/img/cd/7f896a0f05523a07b5dd04a8737879.png)
[OWT] OWT client native P2P E2E test vs2017 construction 4: Construction and link of third-party databases p2pmfc exe

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]

Static code block, code block, constructor execution order

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
![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]

JS Part 4

新版kali切换最高账户

centos7 安装 MySQL 及配置 innodb_ruby

聊聊内存模型和内存序
随机推荐
[advanced Android] entrusted by kotlin
【LeetCode】两数之和II
【机器学习】 吴恩达机器学习作业 ex2逻辑回归 Matlab实现
An implementation of warning bombing
Official announcement! The Hong Kong Zhuhai Macao Bridge is finally here!
Bi skills - authority control
Fetch request details
Summary of some precautions and problems in the use of tars framework (mengxinxiang)
页面导出excel的三种方式
mysql常用指令
CentOS install redis
聊聊内存模型和内存序
MCU model selection for charging point system design
Gx3001q UI instructions for upgrading 8-bit color to 16 bit color
【二分】leetcode1011. Capacity To Ship Packages Within D Days
数据交易怎样实现
innodb_ruby 视角下 MySQL 记录增删改
Svn local computer storage configuration
Record an edusrc vulnerability mining
Mybatties plus batch warehousing