Python AC solution (beat 91% submissions), easy to understand

  • 3
    def removeElements(self, head, val):
        :type head: ListNode
        :type val: int
        :rtype: ListNode
        # Remove the heading nodes
        while head and head.val == val:
            head =
        if not head:
            return head
        prev, cur = head,
        while cur:
            while cur and cur.val == val:
                cur =
   = cur
            if cur:
                prev, cur =,
        return head

  • 0

    Hi Can you please explain it?I am facing difficulty understanding it

  • 0
    This post is deleted!

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.