当前位置:网站首页>Leetcode61. 旋转链表
Leetcode61. 旋转链表
2022-07-28 22:06:00 【Java全栈研发大联盟】
题目传送地址: https://leetcode.cn/problems/rotate-list/
运行效率:
代码如下:
public static ListNode rotateRight(ListNode head, int k) {
//处理边界情况
if(head==null||head.next==null||k==0){
return head;
}
//先要知道链表的长度是多少
int length = 1;
ListNode tailNode = head; //尾结点
//遍历的过程顺便也把尾结点找到
while (tailNode.next != null) {
length++;
tailNode = tailNode.next;
}
//比如链表长度是6,当k=11和k=5旋转以后的效果是一样的
k = k % length;
if(k==0){
return head;
}
//新链表的头结点在原链表中的位置是length-k 新链表的尾结点在原链表中的位置是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;
}
边栏推荐
- 实时数仓:美团点评Flink的实时数仓应用分享
- 2022t elevator repair examination questions and simulation examination
- Manufacturing steps of interactive slide screen in exhibition hall
- 从XSS Payload学习浏览器解码
- YOLOV5学习笔记(一)——原理概述
- EN 1873屋面用装配附件.塑料单个屋面灯—CE认证
- Zibo station construction guide (aigler)
- Arm-a53 data "recommended collection"
- A new generation of ultra safe cellular battery, Sihao aipao, will be available from 139900 yuan
- 深度剖析集成学习GBDT
猜你喜欢
2022 G2 power plant boiler stoker examination question bank simulated examination platform operation
利用递归和链表头插法实现链表成组的翻转——LeetCode25 K个一组翻转链表
电商数据模型设计
Multi sensor fusion positioning (II) -- map based positioning
Xss.haozi.me range details
Okaleido生态核心权益OKA,尽在聚变Mining模式
【CNN】为什么CNN的卷积核大小一般都是奇数
Blocking queue
Pagoda phpMyAdmin unauthorized access vulnerability
pycharm配置运行环境
随机推荐
剑指 Offer 55 - I. 二叉树的深度
Mongodb index add, view, export, delete
Apple's official website is being updated to maintain the apple store. Products such as the iPhone 13 / pro of the Bank of China will enjoy a maximum discount of 600 yuan
Compatibility description between kingbasees and Oracle (4. SQL)
Zero view h5s video platform getUserInfo information disclosure vulnerability cnvd-2020-67113
pip镜像下载
Class, leetcode919 -- complete binary tree inserter
Design idea of room inventory in hotel reservation system database
经典双栈实现队列,注意遍历栈的判定条件修改。
[self] - brush questions BFS
2022t elevator repair examination questions and simulation examination
Genomic DNA isolation Worthington ribonuclease A
双重for循环优化
Deep analysis of integrated learning xgboost
有效供应链管理的八大绩效分析指标(上)
小程序editor富文本编辑使用及rich-text解析富文本
利用递归和链表头插法实现链表成组的翻转——LeetCode25 K个一组翻转链表
2022 G2 power plant boiler stoker examination question bank simulated examination platform operation
Compatibility description between kingbasees and Oracle (5. Pl/sql)
VS2005透过SourceOffSite访问VSS2005的设置方法「建议收藏」