Python双向链表插入操作报AttributeError: 'NoneType' object has no attribute 'prev'错误求助
Fixing the AttributeError in Your Doubly Linked List Insert Method
Hey there! Let's break down what's going wrong with your code and fix it step by step—no jargon, just straightforward explanations.
What's Causing the Error?
The AttributeError: 'NoneType' object has no attribute 'prev' happens when you run doublyLL.insert(3,2) because:
- First, your
location == 1branch (meant for inserting at the end of the list) has the same code as thelocation == 0branch (inserting at the start). This messes up your list structure entirely. - After inserting
2at location 1, your list ends up as2 <-> 1instead of the intended1 <-> 2. - When you try to insert
3at location 2, your code tries to accessnewNode.next.prev—butnewNode.nextisNone(since1is the last node, itsnextpoints toNone), and you can't call.prevon aNonevalue.
Let's Fix the Code
First, correct the location == 1 branch to properly insert at the tail. Then, we'll add small checks to avoid errors if the insertion location is out of bounds.
Here's the revised insert method:
def insert(self, value, location): if self.head is None: print("Linked list does not exist") else: newNode = Node(value) if location == 0: # Insert at the start newNode.prev = None newNode.next = self.head self.head.prev = newNode self.head = newNode elif location == 1: # Insert at the end (fixed logic!) newNode.next = None newNode.prev = self.tail self.tail.next = newNode self.tail = newNode else: # Insert at a specific middle location tempNode = self.head index = 0 # Stop loop if we reach the end of the list early while index < location - 1 and tempNode is not None: tempNode = tempNode.next index += 1 # Only proceed if the location is valid if tempNode is not None: newNode.next = tempNode.next newNode.prev = tempNode # Update the next node's prev only if it exists if tempNode.next is not None: tempNode.next.prev = newNode tempNode.next = newNode else: print("Invalid location—list is shorter than the target position")
Key Fixes Explained
Tail Insertion (
location == 1):- We set
newNode.prevto the currenttail(instead ofself.head) - Update the current tail's
nextto point to the new node - Finally, set
self.tailto the new node so it becomes the new end of the list
- We set
Error Prevention Checks:
- Added
and tempNode is not Noneto the while loop to stop if we reach the end of the list before hitting the target location - Added a check to only execute insertion logic if
tempNodeexists (prevents inserting at a position longer than the list) - Added
if tempNode.next is not Nonebefore accessingtempNode.next.prev—this eliminates the originalNoneerror entirely
- Added
Test the Fixed Code
If you run this revised code:
doublyLL = DoublyLinkedList() doublyLL.createDLL(1) doublyLL.insert(2,1) doublyLL.insert(3,2) # Print the values to verify the list structure print([node.value for node in doublyLL])
You'll get the output [1, 2, 3]—exactly what you expected!
内容的提问来源于stack exchange,提问作者Tahsan Samin
相关产品推荐
相关产品推荐

