Skip to content

203 remove linked list elements

203. Remove Linked List Elements

题目: https://leetcode.com/problems/remove-linked-list-elements/

难度:

Easy

AC代码如下:

class Solution(object):
    def removeElements(self, head, val):
        """
        :type head: ListNode
        :type val: int
        :rtype: ListNode
        """
        dummy = ListNode(-1)
        dummy.next = head

        cur = dummy

        while cur.next:
            if cur.next.val == val:
                cur.next = cur.next.next
            else:
                cur = cur.next

        return dummy.next




回到顶部