当前位置:网站首页>[sword finger offer simple] sword finger offer 24 Reverse linked list
[sword finger offer simple] sword finger offer 24 Reverse linked list
2022-06-12 21:52:00 【Caicai 2022】
subject
Code
Execution time :36 ms, In all Python3 Defeated in submission 72.30% Users of
Memory consumption :16.1 MB, In all Python3 Defeated in submission 34.25% Users of
Pass the test case :27 / 27
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
stack=[]
while head:
stack.append(head)
head=head.next
if not stack:
return None
ans=stack[-1]
now=ans
stack.pop()
while stack:
temp=stack.pop()
now.next=temp
now=temp
now.next=None
return ans
【 Method 2: In situ , No extra space 】
Execution time :40 ms, In all Python3 Defeated in submission 46.78% Users of
Memory consumption :16.1 MB, In all Python3 Defeated in submission 36.94% Users of
Pass the test case :27 / 27
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
if head is None:
return None
pre,now=head,head
while now:
if now==pre:
now=now.next
else:
temp_now=now
temp_now_next=now.next
now.next=pre
pre=temp_now
now=temp_now_next
head.next=None
return pre
【 Method 3: Recursive method 】
Execution time :52 ms, In all Python3 Defeated in submission 9.41% Users of
Memory consumption :20.6 MB, In all Python3 Defeated in submission 10.97% Users of
Pass the test case :27 / 27
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
if head is None or head.next is None:
return head
newHead=self.reverseList(head.next)
head.next.next=head
head.next=None
return newHead
【 Method 4: recursive 】
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
def recur(cur, pre):
if not cur: return pre # Termination conditions
res = recur(cur.next, cur) # Recursive successor node
cur.next = pre # Modify the node reference to
return res # Returns the header node of the inverted linked list
return recur(head, None) # Call recursion and return
边栏推荐
- June training (day 11) - matrix
- The ifeq, filter and strip of makefile are easy to use
- OceanBase 社区版 OCP 功能解读
- Can tonghuashun open an account? Is it safe to open an account in tonghuashun? How to open a securities account
- ICML2022 | GALAXY:極化圖主動學習
- Oracle数据库中查询执行计划的权限
- JUC并发工具包使用指南
- lambda表达式与流优化代码
- Palindrome linked list and linked list intersection problem (intersecting with Xinyi people) do you really know?
- Logstash timestamp converted to UNIX nanosecond nano second time
猜你喜欢

Pixel level reconstruction and restoration technology to solve severe image blur

阅读笔记 Deep Hough Voting for 3D Object Detection in Point Clouds

Shell script Basics

SQL tuning guide notes 10:optimizer statistics concepts

SQL调优指南笔记8:Optimizer Access Paths

SQL调优指南笔记15:Controlling the Use of Optimizer Statistics

SQL调优指南笔记11:Histograms

SQL调优指南笔记6:Explaining and Displaying Execution Plans

关于 安装Qt5.15.2启动QtCreator后“应用程序无法正常启动0xc0000022” 的解决方法

Oracle livelabs experiment: introduction to Oracle Spatial
随机推荐
Ansible-大总结(六)
Ansible playbook和Ansible Roles(三)
NPOI 创建Word
PE安装win10系统
复杂系统如何检测异常?北卡UNCC等最新《复杂分布式系统中基于图的深度学习异常检测方法综述》,阐述最新图异常检测技术进展
jsonUtils
makefile 的ifeq,filter,strip 简单使用
gzip压缩解压缩
(4) Pyqt designs and implements the [factory production management system] order page - add, delete, modify and query (including source code analysis)
Jdbctemplate inserts and returns the primary key
动态规划之如何将问题抽象转化为0-1背包问题(详解利用动态规划求方案数)
SQL调优指南笔记15:Controlling the Use of Optimizer Statistics
Can tonghuashun open an account? Is it safe to open an account in tonghuashun? How to open a securities account
Vagrantbox reinstalling the vboxsf driver
Build a highly available database
Xingda easy control modbustcp to profibusdp
数据批量写入
Cookies and sessions
Cloning PDB with ADG standby
Ansible Roles-项目案例(四)