当前位置:网站首页>Rotating linked list (illustration)
Rotating linked list (illustration)
2022-07-02 08:22:00 【Ischanged】
Leetcode Title Description :
Topic link : Rotate the list
The title is very concise , It's the mobile node , But there is an implied meaning , To move each node of the linked list to the right , Make each node in a new location , Because there is no node behind the tail node , To move, you need to form a ring , All nodes turn in the ring , After moving, disconnect the ring , Form a new linked list . Method : Circular list + Move
The basic idea
:
Calculate the length of the list len, Because when calculating the length, we found the last node of the linked list , The length of the last node does not count , therefore len Starting at 1.
2.Observe the operation of the linked list every len One cycle at a time , Calculation k % len It can effectively reduce the number of times to operate the linked list
( If the chain length is 3, You move each node of the linked list 4 A place , In fact, it just moves each node 1 A place )Find the end node of the linked list , Modify it next The pointer , Connect the linked list into a circular linked list , When calculating the length, you can turn the linked list into a circular linked list at the same time
.Loop through the linked list from the tail node , Until you find the tail node of the linked list after rotation , Put it next Domain set to null, Disconnect the circular linked list from here And return the new head node , Traverse from the tail node len-k%len Once, you can find the tail node of the new linked list , Then find the head node .
Graphic analysis :
We can deduce through one example or several example classes , Conditions for finding the head node precursor after moving the node .
Dynamic diagram demonstration :
The above question is my notes and summary , There are various ways to solve problems with linked lists , This is just a basic method . It's also very simple , It's not as hard to think about using two finger acupuncture , This method is only for the reference of Xiaobai like me .
Reference code :
public Node rotateRight(Node head, int k) {
if (head == null || head.next == null) {
return head;
}
int len=1;
int index;
Node temp = head;
Node newHead;
while (temp.next != null) {
// Calculate the length of the list len, The condition here is that the last node is found , The last node is not calculated , therefore len from 1 Start
len++;
temp = temp.next;
}
/* Usually we find the length , Is to traverse the entire linked list , The condition for finding the length should be temp!=null, But for this problem, our code above not only finds the length, but also finds the last node of the linked list , Make the last node next The field points to the head node and forms a ring , Of course, you can also calculate the length of the linked list normally , But after that, it will take an extra procedure to find the tail node of the linked list . int len=0; while (temp!=null) { len++; temp=temp.next; }*/
temp.next = head; // Connect the linked list into a circular linked list
k %= len; // Rotate the linked list every len One cycle at a time , So calculate k Yes len Surplus of , Avoid repetition
index = len - k; //
while (index-- > 0) {
temp = temp.next;// Find the node to disconnect the circular linked list ,
}
newHead = temp.next;// Record the new head node of the linked list
temp.next = null; // Disconnect the circular linked list
return newHead;
}
️ Too small 🧑*🧑*, Remember to hold ,
边栏推荐
- Cvpr19 deep stacked hierarchical multi patch network for image deblurring paper reproduction
- Library function of C language
- How to uninstall SQL Server cleanly
- Organigramme des activités
- Global and Chinese markets for magnetic resonance imaging (MRI) transmission 2022-2028: Research Report on technology, participants, trends, market size and share
- 力扣每日一题刷题总结:链表篇(持续更新)
- 学习写文章格式
- Chinese garbled code under vscade
- Comparison between setTimeout and requestanimationframe (page refresh)
- CarSim learning experience - rough translation 1
猜你喜欢
Matlab数学建模工具
Chinese garbled code under vscade
Command line is too long
Target detection for long tail distribution -- balanced group softmax
方法递归(斐波那契数列,青蛙跳台阶,汉诺塔问题)
Carsim-路面3D形状文件参数介绍
Carsim-实时仿真的动画同步问题
11月24号,我们为“满月”庆祝
Principes fondamentaux de la théorie musicale (brève introduction)
Dynamic extensible representation for category incremental learning -- der
随机推荐
Development of digital collection trading website development of metauniverse digital collection
Global and Chinese market of tillage finishing machines 2022-2028: Research Report on technology, participants, trends, market size and share
Cvpr19 deep stacked hierarchical multi patch network for image deblurring paper reproduction
[dynamic planning] p4170: coloring (interval DP)
Use of OpenCV 6.4 median filter
静态库和动态库
Global and Chinese market of recovery equipment 2022-2028: Research Report on technology, participants, trends, market size and share
Short video with goods source code, double-click to zoom in when watching the video
程序猿学英语-Learning C
Using super ball embedding to enhance confrontation training
Chinese garbled code under vscade
STM32-新建工程(参考正点原子)
Summary of one question per day: stack and queue (continuously updated)
力扣每日一题刷题总结:栈与队列篇(持续更新)
方法递归(斐波那契数列,青蛙跳台阶,汉诺塔问题)
Matlab数学建模工具
Meta learning Brief
Common shortcut keys of Jupiter notebook (you can also view it by pressing h in command mode)
Jupyter Notebook常用快捷键(在命令模式中按H也可查看)
What are the platforms for selling green label domain names? What is the green label domain name like?