视频讲解
代码
class Solution:
def removeElements(self, head: ListNode, val: int) -> ListNode:
# Solution 1
# dummy_head = ListNode(0)
# dummy_head.next = head
# prev = dummy_head
# curr = head
# while curr:
# if curr.val != val:
# prev, curr = curr, curr.next
# else:
# prev.next = curr.next
# curr = curr.next
# return dummy_head.next
# Solution-2: recursion
# if not head: return head
# head.next = self.removeElements(head.next, val)
# if head.val == val:
# return head.next
# else:
# return head
# Solution 2
if not head: return head
head.next = self.removeElements(head.next, val)
if head.val == val:
return head.next
else:
return head
PPT讲解
本题涉及的知识点:
- [()][]
我的 Leetcode 讲解频道
代码链接
面试高频考点Youtube链接
- 数组(Array)
- 哈希表(Hash Table)
- 链表(Linked List)
- 数学(Math)
- 链表(Linked List)
- 双指针(Two Pointers)
- 字符串(String)
- 二分查找(Binary Search)
- 分治(Divide and Conquer)
- 动态规划(Dynamic Programming)
- 回溯(Backtracking)
- 栈(Stack)
- 堆(Heap)
- 贪心算法(Greedy)
- 排序(Sort)
- 树(Tree)
- 深度优先搜索(Depth-First Search)
- 广度优先搜索(Breadth-First Search)
- 二叉查找树(Binary Search Tree)
- 递归(Recursion)
- 队列(Queue)
- 移动窗口(Sliding Window)