当前位置:网站首页>LeetCode_双指针_中等_61. 旋转链表
LeetCode_双指针_中等_61. 旋转链表
2022-07-06 11:32:00 【小城老街】
1.题目
给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置。
示例 1:
输入:head = [1,2,3,4,5], k = 2
输出:[4,5,1,2,3]
示例 2:
输入:head = [0,1,2], k = 4
输出:[2,0,1]
提示:
链表中节点的数目在范围 [0, 500] 内
-100 <= Node.val <= 100
0 <= k <= 2 * 109
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/rotate-list
2.思路
(1)双指针
详细分析过程见下面代码中的注释。
3.代码实现(Java)
//思路1————双指针
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */
class Solution {
public ListNode rotateRight(ListNode head, int k) {
//考虑特殊情况
if (k == 0 || head == null || head.next == null) {
return head;
}
//计算链表长度
int length = 0;
ListNode aux = head;
while (aux != null) {
aux = aux.next;
length++;
}
// 当 k > length 时,其旋转结果与 k = k % length 一样
k = k % length;
if (k == 0) {
// k % length 如果等于 0,则说明 length 是 k 的整数倍,旋转之后结果与原来相同,故直接返回 head
return head;
}
/* 设 n = length V1 -> V2 -> ... -> V(n-k-1) -> V(n-k) -> ... -> Vn -> null 将链表每个节点向右移动 k 个位置之后,得到: V(n-k) -> ... -> Vn -> V1 -> V2 -> ... -> V(n-k-1) -> null 所以只需要先找到找到节点 V(n-k-1)、V(n-k) 以及 Vn,然后令 V(n-k-1).next = null Vn.next = V1(即head) 最后返回 V(n-k) 即可 V(n-k-1)、V(n-k) 以及 Vn 分别对应下面代码中的 lastNode、newHead 和最后一个while循环结束后的 aux */
aux = head;
ListNode lastNode = head;
int tmp = length - k;
while (tmp > 0) {
aux = aux.next;
if (tmp != 1) {
lastNode = lastNode.next;
}
tmp--;
}
lastNode.next = null;
ListNode newHead = aux;
while (aux.next != null) {
aux = aux.next;
}
aux.next = head;
return newHead;
}
}
边栏推荐
- CPU负载很低,loadavg很高处理方法
- 倒计时2天|腾讯云消息队列数据接入平台(Data Import Platform)直播预告
- 凤凰架构3——事务处理
- GCC [7] - compilation checks the declaration of functions, and link checks the definition bugs of functions
- LeetCode-1279. Traffic light intersection
- ModuleNotFoundError: No module named ‘PIL‘解决方法
- A method of removing text blur based on pixel repair
- R language ggplot2 visualization: use the ggstripchart function of ggpubr package to visualize the grouped dot strip plot, and set the add parameter to add box plots for different levels of dot strip
- 第五期个人能力认证考核通过名单公布
- Digital "new" operation and maintenance of energy industry
猜你喜欢

通俗的讲解,带你入门协程

Mysql Information Schema 学习(一)--通用表

MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!

应用使用Druid连接池经常性断链问题分析

AIRIOT物联网平台赋能集装箱行业构建【焊接工位信息监控系统】

Meilu biological IPO was terminated: the annual revenue was 385million, and Chen Lin was the actual controller

Interface test tool - postman

A method of removing text blur based on pixel repair

CPU负载很低,loadavg很高处理方法
![Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting](/img/dd/c3f4a9c38b156e3a9b9adfd6253773.gif)
Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
随机推荐
R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图
usb host 驱动 - UVC 掉包
全套教学资料,阿里快手拼多多等7家大厂Android面试真题
【pytorch】yolov5 训练自己的数据集
时钟轮在 RPC 中的应用
Excel 中VBA脚本的简单应用
Synchronous development of business and application: strategic suggestions for application modernization
潇洒郎: AttributeError: partially initialized module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipe
How to improve website weight
[translation] a GPU approach to particle physics
R语言使用order函数对dataframe数据进行排序、基于单个字段(变量)进行降序排序(DESCENDING)
Benefit a lot, Android interview questions
包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链
Druid database connection pool details
学习探索-无缝轮播图
深入分析,Android面试真题解析火爆全网
Dark horse -- redis
USB host driver - UVC swap
The second day of rhcsa study
Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform