当前位置:网站首页>Simulation volume leetcode [normal] 061. rotating linked list
Simulation volume leetcode [normal] 061. rotating linked list
2022-07-29 06:56:00 【Encounter simulation volume】
Summary : Simulation volume Leetcode Summary of questions
061. Rotate the list
Give you a list of the head node head , Rotate the list , Move each node of the list to the right k A place .
Example 1:
Input :head = [1,2,3,4,5], k = 2
Output :[4,5,1,2,3]
Example 2:
Input :head = [0,1,2], k = 4
Output :[2,0,1]
Tips :
The number of nodes in the linked list is in the range [0, 500] Inside
-100 <= Node.val <= 100
0 <= k <= 2 * 109
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/rotate-list
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Code :
from leetcode_python.utils import *
k=1
nums = [1,3,4,5]
print(nums[-k:],nums[:-k])
nums = nums[-k:]+nums[:k+1]
print(nums)
def List2Node(datas:List)->ListNode:
""" list -> Linked list """
hair = ListNode(None)
head = hair
for data in datas:
head.next = ListNode(data)
head = head.next
return hair.next
def Node2List(head:ListNode)->List:
""" Linked list -> list """
res = []
while head:
res.append(head.val)
head = head.next
return res
class Solution:
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
if not head or k==0:return head
nums = Node2List(head)
if len(nums)==1:return head
k %= len(nums)
nums = nums[-k:]+nums[:-k]
return List2Node(nums)
def test(data_test):
s = Solution()
data = data_test # normal
# data = [List2Node(data_test[0])] # list turn node
return s.getResult(*data)
def test_obj(data_test):
result = [None]
obj = Solution(*data_test[1][0])
for fun,data in zip(data_test[0][1::],data_test[1][1::]):
if data:
res = obj.__getattribute__(fun)(*data)
else:
res = obj.__getattribute__(fun)()
result.append(res)
return result
if __name__ == '__main__':
datas = [
[],
]
for data_test in datas:
t0 = time.time()
print('-'*50)
print('input:', data_test)
print('output:', test(data_test))
print(f'use time:{
time.time() - t0}s')
remarks :
GitHub:https://github.com/monijuan/leetcode_python
CSDN Summary : Simulation volume Leetcode Summary of questions
You can add QQ Group communication :1092754609
leetcode_python.utils See the description on the summary page for details
First brush questions , Then generated by script blog, If there is any mistake, please leave a message , I see it will be revised ! thank you !
边栏推荐
- 游戏资产的革命
- Software definition boundary SDP
- 数据库多表查询 联合查询 增删改查
- 分享一些你代码更好的小建议,流畅编码提搞效率
- 10 frequently asked JVM questions in interviews
- 模拟卷Leetcode【普通】172. 阶乘后的零
- 猜数字//第一次使用生成随机数
- 【技能积累】presentation实用技巧积累,常用句式
- Teacher wangshuyao's notes on operations research 05 linear programming and simplex method (concept, modeling, standard type)
- 实战!聊聊如何解决MySQL深分页问题
猜你喜欢
Shallow reading of reentrantlock source code of abstractqueuedsynchronizer (AQS)
分享一些你代码更好的小建议,流畅编码提搞效率
如何优雅的写 Controller 层代码?
ECCV 2022丨轻量级模型架ParC-Net 力压苹果MobileViT代码和论文下载
Recurrent neural network RNN
MySQL: what happens in the bufferpool when you crud? Ten pictures can make it clear
Apisik health check test
竣达技术 | 适用于”日月元”品牌UPS微信云监控卡
王树尧老师运筹学课程笔记 04 线性代数基础
IO流 - File - properties
随机推荐
猜数字//第一次使用生成随机数
王树尧老师运筹学课程笔记 04 线性代数基础
新同事写了几段小代码,把系统给搞崩了,被老板爆怼一顿!
Mutual conversion between Base64 and file
线程 - 线程安全 - 线程优化
Security in quantum machine learning
DM数据守护集群搭建
矩阵分解与梯度下降
【技能积累】presentation实用技巧积累,常用句式
Unity免费元素特效推荐
量子机器学习中的安全性问题
吴恩达老师机器学习课程笔记 03 线性代数回顾
【冷冻电镜】RELION4.0 pipeline命令总结(自用)
联邦学习后门攻击总结(2019-2022)
数据库系统概述
Shallow reading of condition object source code
王树尧老师运筹学课程笔记 09 线性规划与单纯形法(单纯形表的应用)
Analysis of four isolation levels of MySQL things
王树尧老师运筹学课程笔记 01 导学与绪论
王树尧老师运筹学课程笔记 08 线性规划与单纯形法(单纯形法)