当前位置:网站首页>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;
}
}
边栏推荐
- 主从搭建报错:The slave I/O thread stops because master and slave have equal MySQL serv
- 三面蚂蚁金服成功拿到offer,Android开发社招面试经验
- 黑马--Redis篇
- Xingnuochi technology's IPO was terminated: it was planned to raise 350million yuan, with an annual revenue of 367million yuan
- 在解决了 2961 个用户反馈后,我做出了这样的改变...
- 思維導圖+源代碼+筆記+項目,字節跳動+京東+360+網易面試題整理
- Word如何显示修改痕迹
- IC设计流程中需要使用到的文件
- Synchronous development of business and application: strategic suggestions for application modernization
- 接雨水问题解析
猜你喜欢

业务与应用同步发展:应用现代化的策略建议

五金机电行业智能供应链管理系统解决方案:数智化供应链为传统产业“造新血”

Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)

The list of people who passed the fifth phase of personal ability certification assessment was published

CPU负载很低,loadavg很高处理方法

【基础架构】Flink/Flink-CDC的部署和配置(MySQL / ES)

Pychrm Community Edition calls matplotlib pyplot. Solution of imshow() function image not popping up

【翻译】云原生观察能力微调查。普罗米修斯引领潮流,但要了解系统的健康状况仍有障碍...

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

An error occurs when installing MySQL: could not create or access the registry key needed for the
随机推荐
打家劫舍III[后序遍历与回溯+动态规划]
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
C language daily practice - day 22: Zero foundation learning dynamic planning
Xingnuochi technology's IPO was terminated: it was planned to raise 350million yuan, with an annual revenue of 367million yuan
Interface test tool - postman
How to do smoke test
Lick the dog until the last one has nothing (simple DP)
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
在解决了 2961 个用户反馈后,我做出了这样的改变...
Carte de réflexion + code source + notes + projet, saut d'octets + jd + 360 + tri des questions d'entrevue Netease
Leetcode topic [array] - 119 Yang Hui triangle II
C # use Marshall to manually create unmanaged memory in the heap and use
USB host driver - UVC swap
GCC [7] - compilation checks the declaration of functions, and link checks the definition bugs of functions
Black Horse - - Redis Chapter
swagger2报错Illegal DefaultValue null for parameter type integer
R language uses DT function to generate t-distribution density function data and plot function to visualize t-distribution density function data
Tensorflow2.0 自定义训练的方式求解函数系数
Swagger2 reports an error illegal DefaultValue null for parameter type integer
接雨水问题解析