当前位置:网站首页>LeetCode_ Double pointer_ Medium_ 61. rotating linked list
LeetCode_ Double pointer_ Medium_ 61. rotating linked list
2022-07-06 19:29:00 【Old street of small town】
1. subject
Give you a list of the head node head , Rotate the list , Move each node of the list to the right k A place .
Example 1:
Input :head = [1,2,3,4,5], k = 2
Output :[4,5,1,2,3]
Example 2:
Input :head = [0,1,2], k = 4
Output :[2,0,1]
Tips :
The number of nodes in the linked list is in the range [0, 500] Inside
-100 <= Node.val <= 100
0 <= k <= 2 * 109
source : Power button (LeetCode)
link :https://leetcode.cn/problems/rotate-list
2. Ideas
(1) Double pointer
See the comments in the following code for the detailed analysis process .
3. Code implementation (Java)
// Ideas 1———— Double pointer
/** * 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) {
// Consider special circumstances
if (k == 0 || head == null || head.next == null) {
return head;
}
// Calculate the length of the list
int length = 0;
ListNode aux = head;
while (aux != null) {
aux = aux.next;
length++;
}
// When k > length when , Its rotation results are consistent with k = k % length equally
k = k % length;
if (k == 0) {
// k % length If it is equal to 0, shows length yes k Integer multiple , After the rotation, the result is the same as the original , So go straight back to head
return head;
}
/* set up n = length V1 -> V2 -> ... -> V(n-k-1) -> V(n-k) -> ... -> Vn -> null Move each node of the list to the right k After two positions , obtain : V(n-k) -> ... -> Vn -> V1 -> V2 -> ... -> V(n-k-1) -> null So you just need to find the node first V(n-k-1)、V(n-k) as well as Vn, Then make V(n-k-1).next = null Vn.next = V1( namely head) Finally back to V(n-k) that will do V(n-k-1)、V(n-k) as well as Vn Corresponding to lastNode、newHead And the last while At the end of the cycle 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;
}
}
边栏推荐
- Reflection and illegalaccessexception exception during application
- DaGAN论文解读
- 如何自定义动漫头像?这6个免费精品在线卡通头像生成器,看一眼就怦然心动!
- 谷粒商城--分布式高级篇P129~P339(完结)
- Zero foundation entry polardb-x: build a highly available system and link the big data screen
- 零基础入门PolarDB-X:搭建高可用系统并联动数据大屏
- 【翻译】数字内幕。KubeCon + CloudNativeCon在2022年欧洲的选择过程
- Detailed idea and code implementation of infix expression to suffix expression
- 五金机电行业供应商智慧管理平台解决方案:优化供应链管理,带动企业业绩增长
- CCNP Part 11 BGP (III) (essence)
猜你喜欢
Tongyu Xincai rushes to Shenzhen Stock Exchange: the annual revenue is 947million Zhang Chi and Su Shiguo are the actual controllers
LeetCode-1279. 红绿灯路口
Application of clock wheel in RPC
Take a look at how cabloyjs workflow engine implements activiti boundary events
Sanmian ant financial successfully got the offer, and has experience in Android development agency recruitment and interview
An error occurs when installing MySQL: could not create or access the registry key needed for the
包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链
ROS custom message publishing subscription example
Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
Live broadcast today | the 2022 Hongji ecological partnership conference of "Renji collaboration has come" is ready to go
随机推荐
Druid database connection pool details
倒计时2天|腾讯云消息队列数据接入平台(Data Import Platform)直播预告
Characteristic colleges and universities, jointly build Netease Industrial College
保证接口数据安全的10种方案
利用 clip-path 绘制不规则的图形
凤凰架构3——事务处理
中缀表达式转后缀表达式详细思路及代码实现
Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
[pytorch] yolov5 train your own data set
CCNP Part 11 BGP (III) (essence)
R language ggplot2 visualization: use the ggdotplot function of ggpubr package to visualize dot plot, set the palette parameter, and set the colors of data points and box graphs of dot plots at differ
DaGAN论文解读
Swagger2 reports an error illegal DefaultValue null for parameter type integer
通俗的讲解,带你入门协程
零基础入门PolarDB-X:搭建高可用系统并联动数据大屏
LeetCode-1279. 红绿灯路口
Take a look at how cabloyjs workflow engine implements activiti boundary events
short i =1; i=i+1与short i=1; i+=1的区别
spark基础-scala
swagger2报错Illegal DefaultValue null for parameter type integer