当前位置:网站首页>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 !
边栏推荐
- NeuralCF-神经协同过滤网络
- 数据库多表查询 联合查询 增删改查
- Relationship between subnet number, host number and subnet mask
- CNN convolutional neural network
- 【备忘】关于ssh为什么会失败的原因总结?下次记得来找。
- 【flask入门系列】Flask-SQLAlchemy的安装与配置
- Mutual conversion between Base64 and file
- How to write controller layer code gracefully?
- Hongke shares | testing and verifying complex FPGA design (2) -- how to perform global oriented simulation in IP core
- 【讲座笔记】如何在稀烂的数据中做深度学习?
猜你喜欢

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

基于噪声伪标签和对抗性学习的医学图像分割注释有效学习

JVM之垃圾回收机制(GC)

5g service interface and reference point

【冷冻电镜|论文阅读】emClarity:用于高分辨率冷冻电子断层扫描和子断层平均的软件

实战!聊聊如何解决MySQL深分页问题

【flask入门系列】Flask-SQLAlchemy的安装与配置

Teacher wangshuyao's operations research course notes 07 linear programming and simplex method (standard form, base, base solution, base feasible solution, feasible base)

Teacher wangshuyao's notes on operations research 04 fundamentals of linear algebra

Jetpack Compose 中的键盘处理
随机推荐
多线程并发下的指令重排问题
Analysis of four isolation levels of MySQL things
DBAsql面试题
Teacher Wu Enda's machine learning course notes 02 univariate linear regression
【冷冻电镜|论文阅读】emClarity:用于高分辨率冷冻电子断层扫描和子断层平均的软件
王树尧老师运筹学课程笔记 05 线性规划与单纯形法(概念、建模、标准型)
【冷冻电镜】Relion4.0——subtomogram教程
Jetpack Compose 中的键盘处理
LDAP brief description and unified authentication description
JMM memory model concept
【flask入门系列】Flask-SQLAlchemy的安装与配置
CDM—码分复用(简单易懂)
Share some tips for better code, smooth coding and improve efficiency
Shallow reading of condition object source code
MySQL: what happens in the bufferpool when you crud? Ten pictures can make it clear
Execution sequence of finally and return
如何优雅的写 Controller 层代码?
10 frequently asked JVM questions in interviews
SS command details
王树尧老师运筹学课程笔记 06 线性规划与单纯形法(几何意义)