Easy to understand Python code


  • 0
    R
    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def removeElements(self, head, val):
            """
            :type head: ListNode
            :type val: int
            :rtype: ListNode
            """
            dummy = ListNode(0)
            dummy.next = head
            
            slower, faster = dummy, dummy.next
            
            while faster:
                if faster.val == val:
                    slower.next, faster = faster.next, faster.next
                else:
                    slower, faster = faster, faster.next
            
            return dummy.next
    

Log in to reply
 

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