当前位置:网站首页>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 !
边栏推荐
- Teacher wangshuyao's notes on operations research course 10 linear programming and simplex method (discussion on detection number and degradation)
- 数据单位:位、字节、字、字长
- Teacher wangshuyao's notes on operations research 03 KKT theorem
- Teacher wangshuyao wrote the notes of operations research course 00 in the front
- SS command details
- Enterprise manager cannot connect to the database instance in Oracle10g solution
- 2022年SQL经典面试题总结(带解析)
- 【冷冻电镜】Relion4.0——subtomogram教程
- Apisik health check test
- 王树尧老师运筹学课程笔记 01 导学与绪论
猜你喜欢

IDEA中实现Mapper接口到映射文件xml的跳转

Why does 5g N2 interface control plane use SCTP protocol?

Let the computer run only one program setting

数据单位:位、字节、字、字长

基于Matlab解决线性规划问题

【冷冻电镜】RELION4.0之subtomogram对位功能源码分析(自用)

Hongke share | bring you a comprehensive understanding of "can bus error" (II) -- can error types

Hongke share | let you have a comprehensive understanding of "can bus errors" (IV) -- producing and recording can errors in practice

Shallow reading of condition object source code

Analysis of four isolation levels of MySQL things
随机推荐
Ali gave several SQL messages and asked how many tree search operations need to be performed?
没那么简单的单例模式
Hongke share | let you have a comprehensive understanding of "can bus errors" (IV) -- producing and recording can errors in practice
模拟卷Leetcode【普通】150. 逆波兰表达式求值
Hongke shares | how to test and verify complex FPGA designs (1) -- entity or block oriented simulation
量子机器学习中的安全性问题
Mutual conversion between Base64 and file
Teacher wangshuyao's notes on operations research 06 linear programming and simplex method (geometric significance)
王树尧老师运筹学课程笔记 03 KKT定理
Teacher Wang Shuyao's notes on operations research 09 linear programming and simplex method (Application of simplex table)
模拟卷Leetcode【普通】222. 完全二叉树的节点个数
PhantomReference 虚引用代码演示
vscode通过remotessh结合xdebug远程调试php解决方案
SQL developer graphical window to create database (tablespace and user)
偏向锁、轻量级锁测试工具类级相关命令
CVPR2022Oral专题系列(一):低光增强
循环神经网络RNN
Phantom reference virtual reference code demonstration
finally 和 return 的执行顺序
10 frequently asked JVM questions in interviews