LeetCode 203. Remove Linked List Elements 解答

2019/06/12 难度 Easy 主题

视频讲解

代码

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链接

面试低频考点Youtube链接

站内搜索 | Search


    视频 | Video Links

    Table of Contents