Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 11 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jul 19, 2023
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit Hold shift + click to select a range
b51a1d8
更新 链表理论基础 排版格式修复
jinbudaily Jul 18, 2023
a781168
更新 0203.移除链表元素 排版格式修复
jinbudaily Jul 18, 2023
61366ff
更新 0707.设计链表 排版格式修复
jinbudaily Jul 18, 2023
32bf073
更新 0206.反转链表 排版格式修复
jinbudaily Jul 18, 2023
2acceb7
更新 024.两两交换链表中的节点 排版格式修复
jinbudaily Jul 18, 2023
a64e11b
更新 019.删除链表的倒数第n个节点 排版格式修复
jinbudaily Jul 18, 2023
8eb214c
面试题 02.07.链表相交 排版格式修复
jinbudaily Jul 18, 2023
17d56ed
更新 0142.环形链表II 排版格式修复
jinbudaily Jul 18, 2023
28b5878
更新 链表总结篇 排版格式修复
jinbudaily Jul 18, 2023
e5ab214
Merge branch 'master' of github.com:jinbudaily/leetcode-master
jinbudaily Jul 18, 2023
ca85e56
Merge pull request #2186 from jinbudaily/master
youngyangyang04 Jul 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
更新 019.删除链表的倒数第n个节点 排版格式修复
  • Loading branch information
jinbudaily committed Jul 18, 2023
commit a64e11b09ad66f4a1ea22cdc3603e5cde85d88ef
39 changes: 24 additions & 15 deletions problems/0019.删除链表的倒数第N个节点.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@



## 19.删除链表的倒数第N个节点
# 19.删除链表的倒数第N个节点

[力扣题目链接](https://leetcode.cn/problems/remove-nth-node-from-end-of-list/)

Expand All @@ -31,10 +31,12 @@
输入:head = [1,2], n = 1
输出:[1]

## 算法公开课

**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[链表遍历学清楚! | LeetCode:19.删除链表倒数第N个节点](https://www.bilibili.com/video/BV1vW4y1U7Gf),相信结合视频再看本篇题解,更有助于大家对链表的理解。**

## 思路

《代码随想录》算法公开课:[链表遍历学清楚! | LeetCode:19.删除链表倒数第N个节点](https://www.bilibili.com/video/BV1vW4y1U7Gf),相信结合视频在看本篇题解,更有助于大家对链表的理解。
## 思路


双指针的经典应用,如果要删除倒数第n个节点,让fast移动n步,然后让fast和slow同时移动,直到fast指向链表末尾。删掉slow所指向的节点就可以了。
Expand Down Expand Up @@ -93,7 +95,7 @@ public:

## 其他语言版本

java:
### Java:

```java
public ListNode removeNthFromEnd(ListNode head, int n){
Expand All @@ -120,7 +122,8 @@ public ListNode removeNthFromEnd(ListNode head, int n){
}
```

Python:
### Python:

```python
# Definition for singly-linked list.
# class ListNode:
Expand Down Expand Up @@ -151,7 +154,8 @@ class Solution:
return dummy_head.next

```
Go:
### Go:

```Go
/**
* Definition for singly-linked list.
Expand All @@ -178,7 +182,7 @@ func removeNthFromEnd(head *ListNode, n int) *ListNode {
}
```

JavaScript:
### JavaScript:

```js
/**
Expand All @@ -198,7 +202,7 @@ var removeNthFromEnd = function(head, n) {
return ret.next;
};
```
TypeScript:
### TypeScript:

版本一(快慢指针法):

Expand Down Expand Up @@ -263,7 +267,7 @@ function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
};
```

Kotlin:
### Kotlin:

```Kotlin
fun removeNthFromEnd(head: ListNode?, n: Int): ListNode? {
Expand All @@ -284,7 +288,8 @@ fun removeNthFromEnd(head: ListNode?, n: Int): ListNode? {
}
```

Swift:
### Swift:

```swift
func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? {
if head == nil {
Expand All @@ -309,8 +314,8 @@ func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? {
}
```

### PHP:

PHP:
```php
function removeNthFromEnd($head, $n) {
// 设置虚拟头节点
Expand All @@ -332,7 +337,8 @@ function removeNthFromEnd($head, $n) {
}
```

Scala:
### Scala:

```scala
object Solution {
def removeNthFromEnd(head: ListNode, n: Int): ListNode = {
Expand All @@ -356,7 +362,8 @@ object Solution {
}
```

Rust:
### Rust:

```rust
impl Solution {
pub fn remove_nth_from_end(head: Option<Box<ListNode>>, mut n: i32) -> Option<Box<ListNode>> {
Expand All @@ -377,7 +384,8 @@ impl Solution {
}
}
```
C语言
### C:

```c
/**c语言单链表的定义
* Definition for singly-linked list.
Expand Down Expand Up @@ -412,7 +420,8 @@ struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {

```

C#:
### C#:

```csharp
public class Solution {
public ListNode RemoveNthFromEnd(ListNode head, int n) {
Expand Down

AltStyle によって変換されたページ (->オリジナル) /