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


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

  • 0
    R

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


  • 0
    G
    This post is deleted!

Log in to reply
 

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