当前位置:网站首页>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;
}
}
边栏推荐
- Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
- Xingnuochi technology's IPO was terminated: it was planned to raise 350million yuan, with an annual revenue of 367million yuan
- Translation D28 (with AC code POJ 26:the nearest number)
- 潇洒郎: AttributeError: partially initialized module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipe
- 三面蚂蚁金服成功拿到offer,Android开发社招面试经验
- 时钟轮在 RPC 中的应用
- [pytorch] yolov5 train your own data set
- 思維導圖+源代碼+筆記+項目,字節跳動+京東+360+網易面試題整理
- Excel 中VBA脚本的简单应用
- pychrm社区版调用matplotlib.pyplot.imshow()函数图像不弹出的解决方法
猜你喜欢

How to improve website weight

Computer network: sorting out common network interview questions (I)

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

CCNP Part 11 BGP (III) (essence)

C language daily practice - day 22: Zero foundation learning dynamic planning

LeetCode-1279. 红绿灯路口

Druid database connection pool details

助力安全人才专业素养提升 | 个人能力认证考核第一阶段圆满结束!

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

渲大师携手向日葵,远控赋能云渲染及GPU算力服务
随机推荐
C # - realize serialization with Marshall class
安装Mysql报错:Could not create or access the registry key needed for the...
Excel 中VBA脚本的简单应用
test about BinaryTree
Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)
Modulenotfounderror: no module named 'PIL' solution
ROS自定义消息发布订阅示例
Reflection and illegalaccessexception exception during application
渲大师携手向日葵,远控赋能云渲染及GPU算力服务
Master Xuan joined hands with sunflower to remotely control enabling cloud rendering and GPU computing services
Problems encountered in using RT thread component fish
Interview assault 63: how to remove duplication in MySQL?
RT-Thread 组件 FinSH 使用时遇到的问题
The nearest library of Qinglong panel
Simple application of VBA script in Excel
终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
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 language uses the order function to sort the dataframe data, and descending sorting based on a single field (variable)
MATLAB中deg2rad和rad2deg函数的使用
Use of map (the data of the list is assigned to the form, and the JSON comma separated display assignment)