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 #526

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 2 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jan 19, 2025
Merged
Changes from 1 commit
Commits
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
Next Next commit
添加19.删除链表的倒数第N个节点,添加了java方法使用递归的方法删除倒数第N个节点
  • Loading branch information
程宣魁 committed Dec 26, 2024
commit 7a1d070c257c16bf01aaf4b95bded65393bc3286
30 changes: 30 additions & 0 deletions problems/0019.删除链表的倒数第N个节点.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,36 @@ class Solution {
}
```


```java
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
// 创建一个新的哑节点,指向原链表头
ListNode s = new ListNode(-1, head);
// 递归调用remove方法,从哑节点开始进行删除操作
remove(s, n);
// 返回新链表的头(去掉可能的哑节点)
return s.next;
}

public int remove(ListNode p, int n) {
// 递归结束条件:如果当前节点为空,返回0
if (p == null) {
return 0;
}
// 递归深入到下一个节点
int net = remove(p.next, n);
// 如果当前节点是倒数第n个节点,进行删除操作
if (net == n) {
p.next = p.next.next;
}
// 返回当前节点的总深度
return net + 1;
}
}
```


### Python:

```python
Expand Down

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