def addAtIndex(self,index,val): if index < 0 or index > self.size: return -1 if index == self.size: self.addAtTail(val) return current = self.head for i in range(index-1): current = current.next new_node = Listnode(val,current,current.next) current.next.prev = new_node current.next = new_node self.size += 1 AttributeError: 'NoneType' object has no attribute 'prev' current.next.prev = new_node 请修改
时间: 2024-02-14 17:10:44 浏览: 71
这段代码中可能存在两个问题:
1. 如果链表为空,即 self.head 为 None,那么在第 9 行的 for 循环中会报错。因此需要在第 3 行加上判断链表是否为空的条件。
2. 如果插入的位置是最后一个结点,即 index == self.size,那么在第 7 行调用 addAtTail 方法后,函数应该返回而不是继续执行。因此需要在第 8 行加上 return 语句。
修改后的代码如下:
```
def addAtIndex(self, index, val):
if index < 0 or index > self.size:
return -1
if index == self.size:
self.addAtTail(val)
return
if self.head is None:
self.addAtHead(val)
return
current = self.head
for i in range(index-1):
current = current.next
new_node = ListNode(val, current, current.next)
current.next.prev = new_node
current.next = new_node
self.size += 1
```
这样就可以避免空指针异常和插入最后一个结点时的错误。
相关问题
class Listnode: def __init__(self,val = 0,next = None): self.val = val self.next = next class MyLinkedList(): def __init__(self): self.dummyHead = Listnode() self.size = 0 def get(self,index): self.dummyHead = self.dummyHead.next for i in range(index): self.dummyHead = self.dummyHead.next return self.dummyHead.val def addAtHead(self,val): new_node = Listnode(val,None) new_node.next = self.dummyHead self.size +=1 """ dummyHead.next = Listnode(val,dummyHead.next) """ def addAtTail(self,val:int)->None: current = self.dummyHead.next while current: current = current.next #current.next = Listnode(val) self.size +=1 def addAtIndex(self,val:int,index:int)->None: current = self.dummyHead.next for i in range(index-1): current = current.next current.next = Listnode(val,current.next) def deleteAtIndex(self,index): current = dummyHead.next for i in range(index-1): current = current.next current.next = current.next.next #输入["MyLinkedList", "addAtHead", "addAtTail", "addAtIndex", "get", "deleteAtIndex", "get"][[], [1], [3], [1, 2], [1], [1], [1]] #输出[null, null, null, null, 2, null, 3] obj = MyLinkedList() obj.addAtHead(1) obj.addAtTail(3) obj.addAtIndex(1,2) param_1 = obj.get(1) obj.deleteAtIndex(1) param_2 = obj.get(1) print(param_1,param_2) 报错'NoneType' object has no attribute 'next'为什么
在addAtTail方法中,当current遍历到链表最后一个节点时,current指向None,而在下一行代码中,current.next是NoneType,因此无法继续执行current.next = Listnode(val)的操作,导致出错。解决方法是在while循环中,将current指向最后一个节点,再执行current.next = Listnode(val)的操作。修改后的代码如下:
```
def addAtTail(self,val:int)->None:
current = self.dummyHead
while current.next:
current = current.next
current.next = Listnode(val,None)
self.size += 1
```
class Stack:#堆栈 class Node: def __init__(self, val, next=None): self.val = val self.next = next def __init__(self): self.top = None def push(self, val): self.top = Stack.Node(val, self.top) def pop(self): assert self.top, 'Stack is empty' val = self.top.val self.top = self.top.next return val def peek(self): return self.top.val if self.top else None def empty(self): return self.top == None def __bool__(self): return not self.empty() def __repr__(self): if not self.top: return '' return '--> ' + ', '.join(str(x) for x in self) def __iter__(self): n = self.top while n: yield n.val n = n.next
这是一个简单的堆栈实现,使用Python语言编写。堆栈是一种常见的数据结构,它具有后进先出(LIFO)的特点。这个堆栈类包含了Node类,用于创建节点,以及push、pop、peek、empty等方法,用于操作堆栈。其中push方法用于将元素压入堆栈,pop方法用于从堆栈中弹出元素并返回,peek方法用于获取堆栈顶部的元素,empty方法用于判断堆栈是否为空。此外,还实现了__bool__和__repr__方法,用于判断堆栈是否为空,并以字符串形式表示堆栈中的元素。
阅读全文