当前位置:网站首页>旋转链表(图解说明)
旋转链表(图解说明)
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;
}
️过小🧑*🧑*,记点持,
边栏推荐
- Library function of C language
- Li Kou daily one question brushing summary: binary tree chapter (continuous update)
- How to back up the configuration before the idea when reinstalling the idea
- Command line is too long
- w10升级至W11系统,黑屏但鼠标与桌面快捷方式能用,如何解决
- Summary of one question per day: String article (continuously updated)
- Matlab-其它
- E-R draw clear content
- Global and Chinese market of recovery equipment 2022-2028: Research Report on technology, participants, trends, market size and share
- Specification for package drawing
猜你喜欢

Where do you find the materials for those articles that have read 10000?

Income in the first month of naked resignation

使用C#语言来进行json串的接收

应对长尾分布的目标检测 -- Balanced Group Softmax

Animation synchronization of CarSim real-time simulation

静态库和动态库
![Open3d learning note 4 [surface reconstruction]](/img/9d/c1c3f2f3d4acd74a2c043571a120b3.png)
Open3d learning note 4 [surface reconstruction]

樂理基礎(簡述)

When a custom exception encounters reflection

cve_ 2019_ 0708_ bluekeep_ Rce vulnerability recurrence
随机推荐
When a custom exception encounters reflection
多站点高可用部署
AR system summary harvest
Matlab other
Target detection for long tail distribution -- balanced group softmax
16: 00 interview, came out at 16:08, the question is really too
C language implements XML generation and parsing library (XML extension)
install.img制作方式
Summary of one question per day: stack and queue (continuously updated)
Rhel7 operation level introduction and switching operation
Chinese garbled code under vscade
使用Matplotlib绘制图表初步
Open3d learning note 5 [rgbd fusion]
Vscode下中文乱码问题
Matlab-其它
Global and Chinese market of tillage finishing machines 2022-2028: Research Report on technology, participants, trends, market size and share
Go functions make, slice, append
How to back up the configuration before the idea when reinstalling the idea
St-link connection error invalid ROM table of STM32 difficult and miscellaneous diseases
WCF更新服务引用报错的原因之一