当前位置:网站首页>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;
}
}
边栏推荐
- 终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
- Php+redis realizes the function of canceling orders over time
- Is not a drawable (color or path): the vector graph downloaded externally cannot be called when it is put into mipmap, and the calling error program crashes
- USB host driver - UVC swap
- 五金机电行业供应商智慧管理平台解决方案:优化供应链管理,带动企业业绩增长
- Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries
- Word如何显示修改痕迹
- Camel case with Hungarian notation
- Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
- Yutai micro rushes to the scientific innovation board: Huawei and Xiaomi fund are shareholders to raise 1.3 billion
猜你喜欢

Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
![[paper notes] transunet: transformers make strongencoders for medical image segmentation](/img/21/3d4710024248b62495e2681ebd1bc4.png)
[paper notes] transunet: transformers make strongencoders for medical image segmentation

Cereals Mall - Distributed Advanced p129~p339 (end)

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

Problems encountered in using RT thread component fish

Take a look at how cabloyjs workflow engine implements activiti boundary events

安装Mysql报错:Could not create or access the registry key needed for the...

Analysis of frequent chain breaks in applications using Druid connection pools

ROS custom message publishing subscription example

Black Horse - - Redis Chapter
随机推荐
Word如何显示修改痕迹
快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
学习探索-使用伪元素清除浮动元素造成的高度坍塌
A method of removing text blur based on pixel repair
R语言ggplot2可视化时间序列柱形图:通过双色渐变配色颜色主题可视化时间序列柱形图
10 schemes to ensure interface data security
保证接口数据安全的10种方案
zabbix 代理服务器 与 zabbix-snmp 监控
Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
Interview assault 63: how to remove duplication in MySQL?
潇洒郎: AttributeError: partially initialized module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipe
Don't miss this underestimated movie because of controversy!
An error occurs when installing MySQL: could not create or access the registry key needed for the
Pytorch common loss function
MRO工业品企业采购系统:如何精细化采购协同管理?想要升级的工业品企业必看!
Pychrm Community Edition calls matplotlib pyplot. Solution of imshow() function image not popping up
test about BinaryTree
R language uses DT function to generate t-distribution density function data and plot function to visualize t-distribution density function data
打家劫舍III[后序遍历与回溯+动态规划]
JDBC详解