当前位置:网站首页>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 ,
边栏推荐
- Development of digital collection trading website development of metauniverse digital collection
- Use of OpenCV 6.4 median filter
- 2022 Heilongjiang's latest eight member (Safety Officer) simulated test question bank and answers
- Using transformer for object detection and semantic segmentation
- 应对长尾分布的目标检测 -- Balanced Group Softmax
- 业务架构图
- I'll show you why you don't need to log in every time you use Taobao, jd.com, etc?
- 11月24号,我们为“满月”庆祝
- Comparable,Comparator,Clonable 接口使用剖析
- 稀疏矩阵存储
猜你喜欢

Carla-UE4Editor导入RoadRunner地图文件(保姆级教程)

Using super ball embedding to enhance confrontation training

Use C language to receive JSON strings

Carsim problem failed to start Solver: Path Id Obj (X) was set to y; Aucune valeur de correction de xxxxx?

Real world anti sample attack against semantic segmentation

Programmers can only be 35? The 74 year old programmer in the United States has been programming for 57 years and has not retired

The internal network of the server can be accessed, but the external network cannot be accessed

Carsim-問題Failed to start Solver: PATH_ID_OBJ(X) was set to Y; no corresponding value of XXXXX?

Use Matplotlib to draw a preliminary chart

Dynamic extensible representation for category incremental learning -- der
随机推荐
SQL操作数据库语法
最长等比子序列
Force buckle method summary: sliding window
Using transformer for object detection and semantic segmentation
STL速查手册
Using super ball embedding to enhance confrontation training
St-link connection error invalid ROM table of STM32 difficult and miscellaneous diseases
Summary of one question per day: String article (continuously updated)
Command line is too long
Li Kou daily one question brushing summary: binary tree chapter (continuous update)
Global and Chinese market of recovery equipment 2022-2028: Research Report on technology, participants, trends, market size and share
Introduction to anti interception technology of wechat domain name
王-课外单词
【无标题】
关于原型图的深入理解
Carsim-实时仿真的动画同步问题
How to apply for a secondary domain name?
STL quick reference manual
SQL server如何卸载干净
Jz-061-serialized binary tree