当前位置:网站首页>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;
}
}
边栏推荐
- Meilu biological IPO was terminated: the annual revenue was 385million, and Chen Lin was the actual controller
- 凤凰架构3——事务处理
- 驼峰式与下划线命名规则(Camel case With hungarian notation)
- Benefit a lot, Android interview questions
- The second day of rhcsa study
- 五金机电行业供应商智慧管理平台解决方案:优化供应链管理,带动企业业绩增长
- Interface test tool - postman
- Reflection and illegalaccessexception exception during application
- R language ggplot2 visualization: use ggviolin function of ggpubr package to visualize violin diagram
- First day of rhcsa study
猜你喜欢

接雨水问题解析

Unlock 2 live broadcast themes in advance! Today, I will teach you how to complete software package integration Issues 29-30

php+redis实现超时取消订单功能

Documents to be used in IC design process

凤凰架构3——事务处理
![Airiot IOT platform enables the container industry to build [welding station information monitoring system]](/img/52/88e3c7b7a60867282921d9bb5c96da.jpg)
Airiot IOT platform enables the container industry to build [welding station information monitoring system]

Sanmian ant financial successfully got the offer, and has experience in Android development agency recruitment and interview

Xingnuochi technology's IPO was terminated: it was planned to raise 350million yuan, with an annual revenue of 367million yuan

spark基础-scala

How to type multiple spaces when editing CSDN articles
随机推荐
主从搭建报错:The slave I/O thread stops because master and slave have equal MySQL serv
Digital "new" operation and maintenance of energy industry
R language uses DT function to generate t-distribution density function data and plot function to visualize t-distribution density function data
R语言dplyr包进行数据分组聚合统计变换(Aggregating transforms)、计算dataframe数据的分组均值(mean)
提前解锁 2 大直播主题!今天手把手教你如何完成软件包集成?|第 29-30 期
The nearest library of Qinglong panel
助力安全人才专业素养提升 | 个人能力认证考核第一阶段圆满结束!
学习探索-使用伪元素清除浮动元素造成的高度坍塌
USB host driver - UVC swap
如何提高网站权重
应用使用Druid连接池经常性断链问题分析
关于静态类型、动态类型、id、instancetype
Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
[translation] a GPU approach to particle physics
谷粒商城--分布式高级篇P129~P339(完结)
C language daily practice - day 22: Zero foundation learning dynamic planning
DaGAN论文解读
A method of removing text blur based on pixel repair
Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
Master Xuan joined hands with sunflower to remotely control enabling cloud rendering and GPU computing services