当前位置:网站首页>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;
}
}
边栏推荐
- 五金机电行业智能供应链管理系统解决方案:数智化供应链为传统产业“造新血”
- 全套教学资料,阿里快手拼多多等7家大厂Android面试真题
- Lucun smart sprint technology innovation board: annual revenue of 400million, proposed to raise 700million
- JDBC details
- Mind map + source code + Notes + project, ByteDance + JD +360+ Netease interview question sorting
- Interface test tool - postman
- Mysql Information Schema 学习(二)--Innodb表
- In 50W, what have I done right?
- 学习探索-无缝轮播图
- LeetCode-1279. 红绿灯路口
猜你喜欢

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

接雨水问题解析
![Looting iii[post sequence traversal and backtracking + dynamic planning]](/img/9b/e9eeed138e46afdeed340bf2629ee1.png)
Looting iii[post sequence traversal and backtracking + dynamic planning]

【翻译】Linkerd在欧洲和北美的采用率超过了Istio,2021年增长118%。

Graffiti intelligence is listed on the dual main board in Hong Kong: market value of 11.2 billion Hong Kong, with an annual revenue of 300 million US dollars
![[玩转Linux] [Docker] MySQL安装和配置](/img/04/6253ef9fdf7d2242b42b4c7fb2c607.png)
[玩转Linux] [Docker] MySQL安装和配置

LeetCode-1279. 红绿灯路口

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

Analysis of frequent chain breaks in applications using Druid connection pools

业务与应用同步发展:应用现代化的策略建议
随机推荐
1805. 字符串中不同整数的数目
How to do smoke test
通俗的讲解,带你入门协程
黑馬--Redis篇
Druid 数据库连接池 详解
PMP每日一练 | 考试不迷路-7.6
【计算情与思】扫地僧、打字员、信息恐慌与奥本海默
利用 clip-path 绘制不规则的图形
今日直播 | “人玑协同 未来已来”2022弘玑生态伙伴大会蓄势待发
LeetCode_双指针_中等_61. 旋转链表
The second day of rhcsa study
Mysql Information Schema 学习(一)--通用表
包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链
Lucun smart sprint technology innovation board: annual revenue of 400million, proposed to raise 700million
The dplyr package of R language performs data grouping aggregation statistical transformations and calculates the grouping mean of dataframe data
Don't miss this underestimated movie because of controversy!
Lick the dog until the last one has nothing (simple DP)
学习探索-无缝轮播图
Interface test tool - postman
The list of people who passed the fifth phase of personal ability certification assessment was published