当前位置:网站首页>旋转链表(图解说明)
旋转链表(图解说明)
2022-07-02 06:28:00 【Ischanged】
Leetcode题目描述:
题目链接:旋转链表
题目很简洁,就是移动节点,但其中有隐含的意思,要将链表的每个节点向右移动,使每个节点在新的位置,由于尾结点后面没有了节点,要移动就要形成环,所有的节点在环里面转,移动完后又把环断开,形成新的链表。方法:环形链表+移动
基本思路
:
计算链表长度len,因为在求长度的时候我们顺带找了链表的最后一个节点,最后一个节点的长度没算,所以len的起始值为1.
2.观察发现对链表的操作每len次循环一次,计算k % len可有效减少操作链表的次数
(如果链表长度为3,你将链表的每个节点移动4个位置,其实只是将每个节点移动了1个位置)找到链表尾节点,修改其next指针,将链表连接成循环链表 ,在求长度的时候就可以同时将链表变为循环链表了
。从尾节点循环遍历链表,直至找到旋转后链表的尾节点,将其next域置为null,从此处断开循环链表 并且返回新的头节点,从尾巴节点遍历len-k%len次即可以找到新链表的尾巴节点了,进而找到头节点。
图示分析:
我们可以通过一个例子或几个例子类推出,移动节点后找到头节点前驱的条件。
动图演示:
上面这题是我刷题的一点笔记与总结,链表解题方式多样,我这个只是一种基本的方法。这种方法也很简单,不像使用双指针法那么难想,这种方法仅供向我一样的小白参考。
参考代码:
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) {
//计算链表长度len,这里的条件是找到了最后一个节点,最后一个节点没有计算,所以len从1开始
len++;
temp = temp.next;
}
/* 平常我们求长度,是遍历整个链表,求长度的条件应该为temp!=null,但这一题我们上面的代码既求了长度也找到了链表的最后一个节点, 使最后一个节点的next 域指向了头结点形成了一个环,当然你也可以按正常地的求链表的长度,但之后要多费一道手续再找到链表的尾结点。 int len=0; while (temp!=null) { len++; temp=temp.next; }*/
temp.next = head; //将链表连接成循环链表
k %= len; //旋转链表每len次循环一次,因此计算k对len的取余,避免重复操作
index = len - k; //
while (index-- > 0) {
temp = temp.next;//找到要断开循环链表的节点,
}
newHead = temp.next;//纪录链表新的头结点
temp.next = null; //断开循环链表
return newHead;
}
️过小🧑*🧑*,记点持,
边栏推荐
- Use Matplotlib to draw a preliminary chart
- SQL server如何卸载干净
- 力扣每日一题刷题总结:链表篇(持续更新)
- SQLyog远程连接centos7系统下的MySQL数据库
- Global and Chinese markets of tilting feeders 2022-2028: Research Report on technology, participants, trends, market size and share
- STL速查手册
- How to back up the configuration before the idea when reinstalling the idea
- Simple implementation scheme of transcoding and streaming (I)
- Open3d learning notes 1 [first glimpse, file reading]
- Use of opencv3 6.2 low pass filter
猜你喜欢
Carsim-問題Failed to start Solver: PATH_ID_OBJ(X) was set to Y; no corresponding value of XXXXX?
Matlab mathematical modeling tool
OpenCV 6.4 中值滤波器的使用
Carsim-问题Failed to start Solver: PATH_ID_OBJ(X) was set to Y; no corresponding value of XXXXX?
St-link connection error invalid ROM table of STM32 difficult and miscellaneous diseases
OpenCV3 6.3 用滤波器进行缩减像素采样
STM32疑难杂症之ST-LINK Connection error INVALID ROM TABLE
Animation synchronization of CarSim real-time simulation
Carsim 学习心得-粗略翻译1
MySQL optimization
随机推荐
Carla-ue4editor import Roadrunner map file (nanny level tutorial)
Vscode下中文乱码问题
包图画法注意规范
Static library and dynamic library
11月24号,我们为“满月”庆祝
Get the width and height of the screen in real time (adaptive)
2022 Heilongjiang latest construction eight members (materialman) simulated examination questions and answers
How to build the alliance chain? How much is the development of the alliance chain
Sqlyog remote connection to MySQL database under centos7 system
The internal network of the server can be accessed, but the external network cannot be accessed
静态库和动态库
Valin cable: BI application promotes enterprise digital transformation
Matlab mathematical modeling tool
Library function of C language
Jupyter Notebook常用快捷键(在命令模式中按H也可查看)
Generate database documents with one click, which can be called swagger in the database industry
MySQL optimization
高中数学必修一
Global and Chinese markets for magnetic resonance imaging (MRI) transmission 2022-2028: Research Report on technology, participants, trends, market size and share
C语言实现XML生成解析库(XML扩展)