当前位置:网站首页>Leetcode 61: rotating linked list
Leetcode 61: rotating linked list
2022-07-04 14:23:00 【Fan Qianzhi】
Title Description
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 * 1 0 9 10^9 109
Ideas
because k It could be worth a lot , So we have to put k except len Remainder .
The essence of this question is to find the penultimate k Nodes , Adjust all the nodes behind it to the front of the linked list .
Find the last k Nodes : Define two nodes , Keep them apart k Nodes , Move back together .
Code
class Solution:
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
l = 0
cur = head
while cur is not None:
l+=1
cur = cur.next
k = k%l
bef = head
aft = head
for i in range(k):
aft = aft.next
while(aft.next is not None):
aft = aft.next
bef = bef.next
# print(bef.val, aft.val)
aft.next = head
res = bef.next
bef.next = None
return res
边栏推荐
- GCC【6】- 编译的4个阶段
- Ws2818m is packaged in cpc8. It is a special circuit for three channel LED drive control. External IC full-color double signal 5v32 lamp programmable LED lamp with outdoor engineering
- 92.(cesium篇)cesium楼栋分层
- R语言dplyr包summarise_if函数计算dataframe数据中所有数值数据列的均值和中位数、基于条件进行数据汇总分析(Summarize all Numeric Variables)
- R language uses the DOTPLOT function of epidisplay package to visualize the frequency of data points in different intervals in the form of point graph, and uses the by parameter to specify the groupin
- 商业智能BI财务分析,狭义的财务分析和广义的财务分析有何不同?
- 一文概览2D人体姿态估计
- Learn kernel 3: use GDB to track the kernel call chain
- 数据湖(十三):Spark与Iceberg整合DDL操作
- Mask wearing detection based on yolov1
猜你喜欢

Understand chisel language thoroughly 12. Chisel project construction, operation and testing (IV) -- chisel test of chisel test

nowcoder重排链表

MySQL之详解索引

Understand chisel language thoroughly 09. Chisel project construction, operation and testing (I) -- build and run chisel project with SBT

vscode 常用插件汇总

docker-compose公网部署redis哨兵模式

Learn kernel 3: use GDB to track the kernel call chain
![[FAQ] summary of common causes and solutions of Huawei account service error 907135701](/img/43/1a9786c89a5ab21d1fb8903cb7b77e.png)
[FAQ] summary of common causes and solutions of Huawei account service error 907135701

Why should Base64 encoding be used for image transmission

测试流程整理(3)
随机推荐
Leetcode 61: 旋转链表
Test evaluation of software testing
【MySQL从入门到精通】【高级篇】(五)MySQL的SQL语句执行流程
2022游戏出海实用发行策略
【信息检索】链接分析
Leetcode T47: 全排列II
codeforce:C. Sum of Substrings【边界处理 + 贡献思维 + 灵光一现】
Popular framework: the use of glide
R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布
数据仓库面试问题准备
海外游戏代投需要注意的
opencv3.2 和opencv2.4安装
商业智能BI财务分析,狭义的财务分析和广义的财务分析有何不同?
docker-compose公网部署redis哨兵模式
【FAQ】華為帳號服務報錯 907135701的常見原因總結和解决方法
AI与生命科学
Data warehouse interview question preparation
DDD application and practice of domestic hotel transactions -- Code
nowcoder重排链表
Install MySQL