当前位置:网站首页>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 ,
边栏推荐
- Jz-061-serialized binary tree
- install.img制作方式
- SQL operation database syntax
- Sequence problem for tqdm and print
- 深入理解JVM
- 力扣方法总结:双指针
- 程序猿学英语-指令式编程
- Global and Chinese markets for magnetic resonance imaging (MRI) transmission 2022-2028: Research Report on technology, participants, trends, market size and share
- 包图画法注意规范
- Sparse matrix storage
猜你喜欢
C语言实现XML生成解析库(XML扩展)
应对长尾分布的目标检测 -- Balanced Group Softmax
Sequence problem for tqdm and print
The internal network of the server can be accessed, but the external network cannot be accessed
CarSim learning experience - rough translation 1
MySQL optimization
Valin cable: BI application promotes enterprise digital transformation
静态库和动态库
Specification for package drawing
W10 is upgraded to W11 system, but the screen is black, but the mouse and desktop shortcuts can be used. How to solve it
随机推荐
W10 is upgraded to W11 system, but the screen is black, but the mouse and desktop shortcuts can be used. How to solve it
The internal network of the server can be accessed, but the external network cannot be accessed
E-R画图明确内容
How to uninstall SQL Server cleanly
Matlab mathematical modeling tool
Global and Chinese markets for conventional rubber track 2022-2028: Research Report on technology, participants, trends, market size and share
Jz-061-serialized binary tree
cve_ 2019_ 0708_ bluekeep_ Rce vulnerability recurrence
深入理解JVM
2022 Heilongjiang latest food safety administrator simulation exam questions and answers
Global and Chinese market of tillage finishing machines 2022-2028: Research Report on technology, participants, trends, market size and share
C language implements XML generation and parsing library (XML extension)
One of the reasons for WCF update service reference error
Static library and dynamic library
OpenCV 6.4 中值滤波器的使用
How to back up the configuration before the idea when reinstalling the idea
Opencv3 6.3 reduced pixel sampling with filters
Real world anti sample attack against semantic segmentation
Global and Chinese market of medicine cabinet 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of snow sweepers 2022-2028: Research Report on technology, participants, trends, market size and share