Python solution

  • 0

    Make a new head instead of old_head.
    Just for avoid to judge the head.

    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    # = None
    class Solution(object):
        def removeElements(self, head, val):
            :type head: ListNode
            :type val: int
            :rtype: ListNode
            dummy = ListNode(1)
   = head
            cur = head
            pre = dummy
            while cur:
                if cur.val == val:
                    pre =
                cur =

Log in to reply

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