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

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 4 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Aug 14, 2024
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
Prev Previous commit
Next Next commit
添加 0203.移除链表元素递归解法 C++ 实现
  • Loading branch information
tiebreaker4869 committed Jul 25, 2024
commit c3541789614a1703154125f3e7a26592f5f2bc5e
28 changes: 28 additions & 0 deletions problems/0203.移除链表元素.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,35 @@ public:
* 时间复杂度: O(n)
* 空间复杂度: O(1)

**也可以通过递归的思路解决本题:**

基础情况:对于空链表,不需要移除元素。

递归情况:首先检查头节点的值是否为 val,如果是则移除头节点,答案即为在头节点的后续节点上递归的结果;如果头节点的值不为 val,则答案为头节点与在头节点的后续节点上递归得到的新链表拼接的结果。

```CPP
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
// 基础情况:空链表
if (head == nullptr) {
return nullptr;
}

// 递归处理
if (head->val == val) {
ListNode* newHead = removeElements(head->next, val);
delete head;
return newHead;
} else {
head->next = removeElements(head->next, val);
return head;
}
}
};
```
* 时间复杂度:O(n)
* 空间复杂度:O(n)


## 其他语言版本
Expand Down

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