当前位置:网站首页>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;
}
}
边栏推荐
- ROS自定义消息发布订阅示例
- Tensorflow and torch code verify whether CUDA is successfully installed
- JDBC详解
- 关于静态类型、动态类型、id、instancetype
- A method of removing text blur based on pixel repair
- 深入分析,Android面试真题解析火爆全网
- A popular explanation will help you get started
- Lucun smart sprint technology innovation board: annual revenue of 400million, proposed to raise 700million
- Translation D28 (with AC code POJ 26:the nearest number)
- Mysql Information Schema 学习(二)--Innodb表
猜你喜欢

Cereals Mall - Distributed Advanced p129~p339 (end)

黑馬--Redis篇

史上超级详细,想找工作的你还不看这份资料就晚了

Tongyu Xincai rushes to Shenzhen Stock Exchange: the annual revenue is 947million Zhang Chi and Su Shiguo are the actual controllers

学习探索-使用伪元素清除浮动元素造成的高度坍塌

Interface test tool - postman

黑马--Redis篇

ROS自定义消息发布订阅示例

C language daily practice - day 22: Zero foundation learning dynamic planning
![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
随机推荐
Dark horse -- redis
English topic assignment (25)
保证接口数据安全的10种方案
如何提高网站权重
今日直播 | “人玑协同 未来已来”2022弘玑生态伙伴大会蓄势待发
Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform
五金机电行业智能供应链管理系统解决方案:数智化供应链为传统产业“造新血”
LeetCode-1279. 红绿灯路口
Lucun smart sprint technology innovation board: annual revenue of 400million, proposed to raise 700million
JDBC details
short i =1; i=i+1与short i=1; i+=1的区别
MATLAB中deg2rad和rad2deg函数的使用
Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
Reflection and illegalaccessexception exception during application
学习探索-函数防抖
第五期个人能力认证考核通过名单公布
Problems encountered in using RT thread component fish
RT-Thread 组件 FinSH 使用时遇到的问题
MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!
The slave i/o thread stops because master and slave have equal MySQL serv