Why do I get RuntimeError?


  • 0
    C

    I don't know how to improve this code to eliminate this error, Line 14: RuntimeError: maximum recursion depth exceeded in cmp.

    # 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
            """
            if head == None:
              return head
    
            if head.next != None and head.next.val == val:
              head.next = head.next.next
    
            self.removeElements(head.next, val)
            return head
    
    

Log in to reply
 

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