当前位置:网站首页>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
边栏推荐
- What is the difference between Bi financial analysis in a narrow sense and financial analysis in a broad sense?
- Gorm read / write separation (rotation)
- Basic mode of service mesh
- 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
- Matters needing attention in overseas game Investment Agency
- LifeCycle
- 去除重复字母[贪心+单调栈(用数组+len来维持单调序列)]
- Idea shortcut keys
- File creation, writing, reading, deletion (transfer) in go language
- Detailed index of MySQL
猜你喜欢
Understand chisel language thoroughly 06. Chisel Foundation (III) -- registers and counters
统计php程序运行时间及设置PHP最长运行时间
C # WPF realizes the real-time screen capture function of screen capture box
失败率高达80%,企业数字化转型路上有哪些挑战?
一文概览2D人体姿态估计
Intelligence d'affaires bi analyse financière, analyse financière au sens étroit et analyse financière au sens large sont - ils différents?
The failure rate is as high as 80%. What are the challenges on the way of enterprise digital transformation?
leetcode:6109. 知道秘密的人数【dp的定义】
富文本编辑:wangEditor使用教程
数据仓库面试问题准备
随机推荐
Rich text editing: wangeditor tutorial
Learning projects are self-made, and growth opportunities are self created
Excel快速合并多行数据
瑞吉外卖笔记
Idea shortcut keys
实战解惑 | OpenCV中如何提取不规则ROI区域
【信息检索】链接分析
R language dplyr package summary_ If function calculates the mean and median of all numerical data columns in dataframe data, and summarizes all numerical variables based on conditions
[MySQL from introduction to proficiency] [advanced chapter] (V) SQL statement execution process of MySQL
R language uses follow up of epidisplay package The plot function visualizes the longitudinal follow-up map of multiple ID (case) monitoring indicators, and uses stress The col parameter specifies the
Test evaluation of software testing
第十七章 进程内存
利用Shap值进行异常值检测
The game goes to sea and operates globally
测试流程整理(2)
Basic mode of service mesh
PHP log debugging
10.(地图数据篇)离线地形数据处理(供Cesium使用)
聊聊保证线程安全的 10 个小技巧
RK1126平台OSD的实现支持颜色半透明度多通道支持中文