当前位置:网站首页>模拟卷Leetcode【普通】061. 旋转链表
模拟卷Leetcode【普通】061. 旋转链表
2022-07-29 05:39:00 【邂逅模拟卷】
汇总:模拟卷Leetcode 题解汇总
061. 旋转链表
给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置。
示例 1:
输入:head = [1,2,3,4,5], k = 2
输出:[4,5,1,2,3]
示例 2:
输入:head = [0,1,2], k = 4
输出:[2,0,1]
提示:
链表中节点的数目在范围 [0, 500] 内
-100 <= Node.val <= 100
0 <= k <= 2 * 109
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/rotate-list
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码:
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:
"""列表 -> 链表"""
hair = ListNode(None)
head = hair
for data in datas:
head.next = ListNode(data)
head = head.next
return hair.next
def Node2List(head:ListNode)->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转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')
备注:
GitHub:https://github.com/monijuan/leetcode_python
CSDN汇总:模拟卷Leetcode 题解汇总
可以加QQ群交流:1092754609
leetcode_python.utils详见汇总页说明
先刷的题,之后用脚本生成的blog,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- IDEA中实现Mapper接口到映射文件xml的跳转
- 如何画出优秀的架构图
- MySql基础知识(高频面试题)
- C语言数据类型
- 最新PyCharm2018破解教程
- Joint modeling of price preference and interest preference in conversation recommendation - extensive reading of papers
- 【冷冻电镜】RELION4.0 pipeline命令总结(自用)
- 成长为架构师途中的一些思考
- 王树尧老师运筹学课程笔记 08 线性规划与单纯形法(单纯形法)
- Neuralcf neural collaborative filtering network
猜你喜欢

AbstractQueuedSynchronizer(AQS) 之共享锁源码浅读

DM数据守护集群搭建

10种常见的软件架构模式

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

【论文阅读 | 冷冻电镜】RELION 4.0 中新的 subtomogram averaging 方法解读

C语言内存-栈与堆使用

The core of openresty and cosocket

Loss function -- cross entropy loss function

C language memory stack and heap usage

分享一些你代码更好的小建议,流畅编码提搞效率
随机推荐
王树尧老师运筹学课程笔记 09 线性规划与单纯形法(单纯形表的应用)
ECCV 2022丨轻量级模型架ParC-Net 力压苹果MobileViT代码和论文下载
Hongke share | let you have a comprehensive understanding of "can bus errors" (IV) -- producing and recording can errors in practice
LDAP brief description and unified authentication description
CNN-卷积神经网络
【论文阅读 | 冷冻电镜】RELION 4.0 中新的 subtomogram averaging 方法解读
Annotation
【冷冻电镜|论文阅读】emClarity:用于高分辨率冷冻电子断层扫描和子断层平均的软件
Navicat for Oracle Cannot create oci environment
【经验】通过跳板机远程连接内网服务器的相关配置
Shallow reading of shared lock source code of abstractqueuedsynchronizer (AQS)
Teacher wangshuyao wrote the notes of operations research course 00 in the front
王树尧老师运筹学课程笔记 02 高等数学基础
The difference between pairs and ipairs
Computer right mouse click always turn around what's going on
多线程并发下的指令重排问题
NLP word segmentation
Difference between CNAME record and a record
5G服务化接口和参考点
SDN topology discovery principle