当前位置:网站首页>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
边栏推荐
- 失败率高达80%,企业数字化转型路上有哪些挑战?
- [FAQ] summary of common causes and solutions of Huawei account service error 907135701
- nowcoder重排链表
- MATLAB中tiledlayout函数使用
- 瑞吉外卖笔记
- Intelligence d'affaires bi analyse financière, analyse financière au sens étroit et analyse financière au sens large sont - ils différents?
- Code hoof collection of wonderful secret place
- ML之shap:基于boston波士顿房价回归预测数据集利用Shap值对LiR线性回归模型实现可解释性案例
- golang fmt. Printf() (turn)
- 数据中台概念
猜你喜欢

按照功能对Boost库进行分类

RK1126平台OSD的实现支持颜色半透明度多通道支持中文
![[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

数据仓库面试问题准备

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

Vscode common plug-ins summary

leetcode:6109. 知道秘密的人数【dp的定义】

How to package QT and share exe

The implementation of OSD on rk1126 platform supports color translucency and multi-channel support for Chinese

docker-compose公网部署redis哨兵模式
随机推荐
Common content type correspondence table
Introducing testfixture into unittest framework
NowCoder 反转链表
为什么图片传输要使用base64编码
2022游戏出海实用发行策略
统计php程序运行时间及设置PHP最长运行时间
Oppo find N2 product form first exposure: supplement all short boards
Understand chisel language thoroughly 03. Write to the developer of Verilog to chisel (you can also see it without Verilog Foundation)
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
Leetcode T48:旋转图像
Mongodb commonly used 28 query statements (forward)
Abnormal value detection using shap value
Map of mL: Based on Boston house price regression prediction data set, an interpretable case of xgboost model using map value
Understand chisel language thoroughly 09. Chisel project construction, operation and testing (I) -- build and run chisel project with SBT
基于51单片机的超声波测距仪
AI and Life Sciences
MATLAB中tiledlayout函数使用
Some problems and ideas of data embedding point
【云原生】我怎么会和这个数据库杠上了?
实战解惑 | OpenCV中如何提取不规则ROI区域