当前位置:网站首页>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
边栏推荐
- Install and use MAC redis, connect to remote server redis
- 利用Shap值进行异常值检测
- ML:SHAP值的简介、原理、使用方法、经典案例之详细攻略
- R language uses the mutation function of dplyr package to standardize the specified data column (using mean function and SD function), and calculates the grouping mean of the standardized target varia
- 富文本编辑:wangEditor使用教程
- 架构方面的进步
- 【FAQ】華為帳號服務報錯 907135701的常見原因總結和解决方法
- sql优化之explain
- 【云原生】我怎么会和这个数据库杠上了?
- ML之shap:基于boston波士顿房价回归预测数据集利用shap值对XGBoost模型实现可解释性案例
猜你喜欢
Map of mL: Based on Boston house price regression prediction data set, an interpretable case of xgboost model using map value
Real time data warehouse
Use of tiledlayout function in MATLAB
gin集成支付宝支付
Product identification of intelligent retail cabinet based on paddlex
NowCoder 反转链表
Detailed index of MySQL
【MySQL从入门到精通】【高级篇】(五)MySQL的SQL语句执行流程
[MySQL from introduction to proficiency] [advanced chapter] (V) SQL statement execution process of MySQL
Excel快速合并多行数据
随机推荐
富文本编辑:wangEditor使用教程
Product identification of intelligent retail cabinet based on paddlex
Learn kernel 3: use GDB to track the kernel call chain
Error in find command: paths must precede expression (turn)
Common content type correspondence table
Use of arouter
ML之shap:基于boston波士顿房价回归预测数据集利用shap值对XGBoost模型实现可解释性案例
Map of mL: Based on Boston house price regression prediction data set, an interpretable case of xgboost model using map value
Supprimer les lettres dupliquées [avidité + pile monotone (maintenir la séquence monotone avec un tableau + Len)]
How to package QT and share exe
Understand chisel language thoroughly 04. Chisel Foundation (I) - signal type and constant
Rich text editing: wangeditor tutorial
测试流程整理(3)
Detailed index of MySQL
商业智能BI财务分析,狭义的财务分析和广义的财务分析有何不同?
Assertion of unittest framework
Install and use MAC redis, connect to remote server redis
What is the real meaning and purpose of doing things, and what do you really want
R语言使用epiDisplay包的followup.plot函数可视化多个ID(病例)监测指标的纵向随访图、使用stress.col参数指定强调线的id子集的颜色(色彩)
gin集成支付宝支付