当前位置:网站首页>Leetcode61. rotating linked list
Leetcode61. rotating linked list
2022-07-29 00:01:00 【Java full stack R & D Alliance】
Title transmission address : https://leetcode.cn/problems/rotate-list/
Operational efficiency :
The code is as follows :
public static ListNode rotateRight(ListNode head, int k) {
// Dealing with border situations
if(head==null||head.next==null||k==0){
return head;
}
// First, you need to know the length of the linked list
int length = 1;
ListNode tailNode = head; // Caudal node
// The traversal process also finds the tail node
while (tailNode.next != null) {
length++;
tailNode = tailNode.next;
}
// For example, the length of the linked list is 6, When k=11 and k=5 The effect of rotation is the same
k = k % length;
if(k==0){
return head;
}
// The position of the head node of the new linked list in the original linked list is length-k The position of the tail node of the new linked list in the original linked list is length-k
ListNode newTailNode = head;
int index = 0;
while (index < length - k-1) {
newTailNode = newTailNode.next;
index++;
}
ListNode newHeadNode = newTailNode.next;
newTailNode.next = null;
tailNode.next = head;
return newHeadNode;
}
边栏推荐
- Js判断数据类型的4种⽅式
- GhostNets on Heterogeneous Devices via Cheap Operations
- 【TA-霜狼_may-《百人计划》】图形3.6 纹理压缩——包体瘦身术
- Leetcode 763. partition labels divide alphabetic intervals (medium)
- Multisensor fusion positioning (III) -- inertial technology
- pip镜像下载
- Wildcard ssl/tls certificate
- 软件设计师的错题汇总
- Multi sensor fusion positioning (I) -- 3D laser odometer
- 1-6 state and binding events
猜你喜欢

Urease -- Characteristics and determination scheme of Worthington jack bean urease

Equipped with a new generation of ultra safe cellular batteries, Sihao aipao is available from 139900 yuan

PowerCLi 批量添加esxi到vCenter

GhostNets on Heterogeneous Devices via Cheap Operations

实时数仓:网易严选基于Flink的实时数仓实践

使用Pytorch快速训练网络模型

DevOps在物联网解决方案中的应用

Worthington -- Specification of Worthington trypsin inhibitor

Android studio连接MySQL并完成简单的登录注册功能

Build SSM project with JSP as view parser
随机推荐
RHCE the next day
GhostNets on Heterogeneous Devices via Cheap Operations
EN 12101-8:2011 smoke dampers for smoke and heat control systems - CE certification
请简述list,set,map类型的集合的各自特点(简述三种不同的继承方式下)
Where is the DP interface of desktop computer (what if the host has no DP interface)
Compose 的声明式代码如此简洁?
【MySQL 8】Generated Invisible Primary Keys(GIPK)
台式机dp接口在哪(主机没有dp接口怎么办)
What is in word?:^ p
Uricase - Characteristics of uricase in Worthington pig liver:
Doip test development practice
2022-07-28:以下go语言代码输出什么?A:AA;B:AB;C:BA;D:BB。 package main import ( “fmt“ ) func
Arm-A53资料「建议收藏」
hutool官网(hutool好用吗)
[TA frost wolf \u may - "hundred people plan"] Figure 3.6 texture compression - inclusion slimming
多传感器融合定位(一)——3D激光里程计
实时数仓:美团点评Flink的实时数仓应用分享
1-7 solve the problem of this pointing of methods in classes
SAP temporary tablespace error handling
1-8 basic use of props