当前位置:网站首页>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 !
边栏推荐
- 王树尧老师运筹学课程笔记 05 线性规划与单纯形法(概念、建模、标准型)
- Difference between CNAME record and a record
- SQL developer graphical window to create database (tablespace and user)
- Etcd principle
- Share some tips for better code, smooth coding and improve efficiency
- Federal learning backdoor attack summary (2019-2022)
- 吴恩达老师机器学习课程笔记 04 多元线性回归
- Understanding of access, hybrid and trunk modes
- 新同事写了几段小代码,把系统给搞崩了,被老板爆怼一顿!
- 5g service interface and reference point
猜你喜欢

CVPR2022Oral专题系列(一):低光增强

CNN-卷积神经网络

Hongke shares | how to test and verify complex FPGA designs (1) -- entity or block oriented simulation

Ping principle

Actual combat! Talk about how to solve the deep paging problem of MySQL

Analysis of four isolation levels of MySQL things

Apisik health check test

Federal learning backdoor attack summary (2019-2022)

C language memory stack and heap usage

分享一些你代码更好的小建议,流畅编码提搞效率
随机推荐
Use of PDO
Federal learning backdoor attack summary (2019-2022)
'function VTable for error: undefined reference to... 'cause and solution of the problem
Recurrent neural network RNN
王树尧老师运筹学课程笔记 01 导学与绪论
Shallow reading of reentrantlock source code of abstractqueuedsynchronizer (AQS)
多线程并发下的指令重排问题
JVM之垃圾回收机制(GC)
Hongke share | let you have a comprehensive understanding of "can bus error" (III) -- can node status and error counter
循环神经网络RNN
分享一些你代码更好的小建议,流畅编码提搞效率
【论文阅读 | cryoET】Gum-Net:快速准确的3D Subtomo图像对齐和平均的无监督几何匹配
【技能积累】写邮件时的常用表达
【冷冻电镜|论文阅读】子断层平均 M 软件解读:Multi-particle cryo-EM refinement with M
Not so simple singleton mode
CNN convolutional neural network
ECCV 2022丨轻量级模型架ParC-Net 力压苹果MobileViT代码和论文下载
Embedding理解+代码
【冷冻电镜】Relion4.0——subtomogram教程
Enterprise manager cannot connect to the database instance in Oracle10g solution