当前位置:网站首页>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;
}
边栏推荐
- Compatibility description between kingbasees and Oracle (5. Pl/sql)
- DevOps在物联网解决方案中的应用
- 数仓:Doris在美团的应用实践
- 有效供应链管理的八大绩效分析指标(上)
- 多传感器融合定位(二)——基于地图的定位
- Wildcard ssl/tls certificate
- Uricase - Characteristics of uricase in Worthington pig liver:
- Doip communication of canoe application case
- Leetcode59. 螺旋矩阵 II
- Compatibility description between kingbasees and Oracle (4. SQL)
猜你喜欢

Linux之yum安装MySQL

EN 1873屋面用装配附件.塑料单个屋面灯—CE认证

After SAP Oracle replicates a new instance, the remote connection of the database reports an error ora-01031

连接池-归还连接详解(下)

Leetcode62. 不同路径

Worthington丨Worthington胰蛋白酶抑制剂说明书

Worthington RNA determination detailed introduction

Powercli VMware vCenter deploys conventional new VMS in batch through self built PXE server with one click

【C】替换空格,宏实现整数的二进制奇偶位交换

多传感器融合定位(三)——惯性技术
随机推荐
Multi sensor fusion positioning (II) -- map based positioning
SAP 临时表空间错误处理
1-4 类的复习
Type 1-5 components
Multi sensor fusion positioning (I) -- 3D laser odometer
pycharm新建项目
实时数仓:网易严选基于Flink的实时数仓实践
【C】喝汽水,找单身狗问题
【MySQL 8】Generated Invisible Primary Keys(GIPK)
Multisensor fusion positioning (III) -- inertial technology
SAP oracle 复制新实例后数据库远程连接报错 ora-01031
Oracle create tablespaces and users
PowerCLi 批量添加esxi到vCenter
PowerCL 批量创建及管理虚拟交换机
SAP temporary tablespace error handling
【TA-霜狼_may-《百人计划》】美术2.2 模型基础
Js判断数据类型的4种⽅式
Jincang database kingbasees client Programming Interface Guide - ODBC feature support constraints
Leetcode63. 不同路径 II
Worthington核糖核酸酶B历史和化学性质说明