forked from youngyangyang04/leetcode-master
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
623448b
commit 9f090e6
Showing
21 changed files
with
2,719 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
|
||
|
||
<p align="center"> | ||
<a href="https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ"><img src="https://img.shields.io/badge/知识星球-代码随想录-blue" alt=""></a> | ||
<a href="https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw"><img src="https://img.shields.io/badge/刷题-微信群-green" alt=""></a> | ||
<a href="https://img-blog.csdnimg.cn/20201210231711160.png"><img src="https://img.shields.io/badge/公众号-代码随想录-brightgreen" alt=""></a> | ||
<a href="https://space.bilibili.com/525438321"><img src="https://img.shields.io/badge/B站-代码随想录-orange" alt=""></a> | ||
</p> | ||
|
||
## 19.删除链表的倒数第N个节点 | ||
|
||
## 思路 | ||
|
||
双指针的经典应用,如果要删除倒数第n个节点,让fast移动n步,然后让fast和slow同时移动,直到fast指向链表末尾。删掉slow所指向的节点就可以了。 | ||
|
||
思路是这样的,但要注意一些细节。 | ||
|
||
分为如下几步: | ||
|
||
* 首先这里我推荐大家使用虚拟头结点,这样方面处理删除实际头结点的逻辑,如果虚拟头结点不清楚,可以看这篇: [链表:听说用虚拟头节点会方便很多?](https://mp.weixin.qq.com/s/slM1CH5Ew9XzK93YOQYSjA) | ||
|
||
|
||
* 定义fast指针和slow指针,初始值为虚拟头结点,如图: | ||
|
||
<img src='https://code-thinking.cdn.bcebos.com/pics/19.%E5%88%A0%E9%99%A4%E9%93%BE%E8%A1%A8%E7%9A%84%E5%80%92%E6%95%B0%E7%AC%ACN%E4%B8%AA%E8%8A%82%E7%82%B9.png' width=600> </img></div> | ||
|
||
* fast首先走n + 1步 ,为什么是n+1呢,因为只有这样同时移动的时候slow才能指向删除节点的上一个节点(方便做删除操作),如图: | ||
<img src='https://code-thinking.cdn.bcebos.com/pics/19.%E5%88%A0%E9%99%A4%E9%93%BE%E8%A1%A8%E7%9A%84%E5%80%92%E6%95%B0%E7%AC%ACN%E4%B8%AA%E8%8A%82%E7%82%B91.png' width=600> </img></div> | ||
|
||
* fast和slow同时移动,之道fast指向末尾,如题: | ||
<img src='https://code-thinking.cdn.bcebos.com/pics/19.%E5%88%A0%E9%99%A4%E9%93%BE%E8%A1%A8%E7%9A%84%E5%80%92%E6%95%B0%E7%AC%ACN%E4%B8%AA%E8%8A%82%E7%82%B92.png' width=600> </img></div> | ||
|
||
* 删除slow指向的下一个节点,如图: | ||
<img src='https://code-thinking.cdn.bcebos.com/pics/19.%E5%88%A0%E9%99%A4%E9%93%BE%E8%A1%A8%E7%9A%84%E5%80%92%E6%95%B0%E7%AC%ACN%E4%B8%AA%E8%8A%82%E7%82%B93.png' width=600> </img></div> | ||
|
||
此时不难写出如下C++代码: | ||
|
||
```C++ | ||
class Solution { | ||
public: | ||
ListNode* removeNthFromEnd(ListNode* head, int n) { | ||
ListNode* dummyHead = new ListNode(0); | ||
dummyHead->next = head; | ||
ListNode* slow = dummyHead; | ||
ListNode* fast = dummyHead; | ||
while(n-- && fast != NULL) { | ||
fast = fast->next; | ||
} | ||
fast = fast->next; // fast再提前走一步,因为需要让slow指向删除节点的上一个节点 | ||
while (fast != NULL) { | ||
fast = fast->next; | ||
slow = slow->next; | ||
} | ||
slow->next = slow->next->next; | ||
return dummyHead->next; | ||
} | ||
}; | ||
``` | ||
## 其他语言补充 | ||
java: | ||
```java | ||
class Solution { | ||
public ListNode removeNthFromEnd(ListNode head, int n) { | ||
ListNode dummy = new ListNode(-1); | ||
dummy.next = head; | ||
ListNode slow = dummy; | ||
ListNode fast = dummy; | ||
while (n-- > 0) { | ||
fast = fast.next; | ||
} | ||
// 记住 待删除节点slow 的上一节点 | ||
ListNode prev = null; | ||
while (fast != null) { | ||
prev = slow; | ||
slow = slow.next; | ||
fast = fast.next; | ||
} | ||
// 上一节点的next指针绕过 待删除节点slow 直接指向slow的下一节点 | ||
prev.next = slow.next; | ||
// 释放 待删除节点slow 的next指针, 这句删掉也能AC | ||
slow.next = null; | ||
return dummy.next; | ||
} | ||
} | ||
``` | ||
|
||
------------------------ | ||
|
||
* 微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) | ||
* B站:[代码随想录](https://space.bilibili.com/525438321) | ||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) | ||
|
||
![](../pics/公众号.png) | ||
|
Oops, something went wrong.