leetcode, 用python为什么提交时报错:AttributeError: 'NoneType' object has no attribute 'next' 啊
#Definitionforsingly-linkedlist.#classListNode(object):#def__init__(self,x):#self.val...
# 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
"""
cur = ListNode(0)
cur.next = head
p = cur
while p.next:
if p.next.val == val:
cur.next = cur.next.next
else:
p = p.next
return cur.next 展开
# 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
"""
cur = ListNode(0)
cur.next = head
p = cur
while p.next:
if p.next.val == val:
cur.next = cur.next.next
else:
p = p.next
return cur.next 展开
2个回答
展开全部
估计是solution的问题,改动一下代码就可以了:
class Solution(object):
def removeElements(self, head, val):
"""
:type head: ListNode
:type val: int
:rtype: ListNode
"""
cur = ListNode(0)
cur.next = head
p = cur
cur.next=None
while p.next:
if p.next.val == val:
cur.next=p.next
p.next = p.next.next
break
p = p.next
return cur.next
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询