当前位置:网站首页>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
边栏推荐
- Remove duplicate letters [greedy + monotonic stack (maintain monotonic sequence with array +len)]
- Map of mL: Based on Boston house price regression prediction data set, an interpretable case of xgboost model using map value
- GCC [6] - 4 stages of compilation
- flink sql-client.sh 使用教程
- Data center concept
- 海外游戏代投需要注意的
- Excel快速合并多行数据
- Golang uses JSON unmarshal number to interface{} number to become float64 type (turn)
- sql优化之explain
- 【算法leetcode】面试题 04.03. 特定深度节点链表(多语言实现)
猜你喜欢

数据湖(十三):Spark与Iceberg整合DDL操作

Understand chisel language thoroughly 10. Chisel project construction, operation and testing (II) -- Verilog code generation in chisel & chisel development process

Understand chisel language thoroughly 11. Chisel project construction, operation and test (III) -- scalatest of chisel test

测试流程整理(2)

统计php程序运行时间及设置PHP最长运行时间

gin集成支付宝支付

Leetcode 61: 旋转链表
![[FAQ] Huawei Account Service Error Report 907135701 Common reasons Summary and Solutions](/img/43/1a9786c89a5ab21d1fb8903cb7b77e.png)
[FAQ] Huawei Account Service Error Report 907135701 Common reasons Summary and Solutions

Test process arrangement (2)

实时数据仓库
随机推荐
Error in find command: paths must precede expression (turn)
海外游戏代投需要注意的
Xcode 异常图片导致ipa包增大问题
Some problems and ideas of data embedding point
leetcode:6110. 网格图中递增路径的数目【dfs + cache】
Common content type correspondence table
第十六章 字符串本地化和消息字典(二)
Blob, text geometry or JSON column'xxx'can't have a default value query question
利用Shap值进行异常值检测
flink sql-client.sh 使用教程
游戏出海,全球化运营
Progress in architecture
Supprimer les lettres dupliquées [avidité + pile monotone (maintenir la séquence monotone avec un tableau + Len)]
Code hoof collection of wonderful secret place
Excel quickly merges multiple rows of data
[MySQL from introduction to proficiency] [advanced chapter] (V) SQL statement execution process of MySQL
R language ggplot2 visualization: gganimate package creates dynamic line graph animation (GIF) and uses transition_ The reveal function displays data step by step along a given dimension in the animat
docker-compose公网部署redis哨兵模式
MySQL之详解索引
Product identification of intelligent retail cabinet based on paddlex