NameError: name 'mergelist'未定义求助:已定义仍报错
Let's break down why you're hitting this error and fix your code step by step:
First off, the core issue here is that mergelist is an instance method inside your linkedlist class, but you're probably trying to call it like a standalone global function (e.g., mergelist(l1, l2) instead of using a linkedlist instance to invoke it). On top of that, your code has a few other bugs that'll cause problems once you fix the NameError—let's tackle all of them.
1. Fix how you call mergelist
Since mergelist belongs to the linkedlist class, you need to create an instance of linkedlist first, then call the method through that instance. Also, note that your mergelist expects two node pointers (the heads of the two lists you want to merge), not the linkedlist instances themselves. Here's how to do it correctly:
# Set up your two input linked lists ll1 = linkedlist() ll1.push(3) ll1.push(2) ll1.push(1) ll2 = linkedlist() ll2.push(6) ll2.push(5) ll2.push(4) # Create a linkedlist instance to handle the merge merged_list = linkedlist() # Pass the HEAD nodes of your two lists to mergelist merged_list.head = merged_list.mergelist(ll1.head, ll2.head)
2. Fix other bugs in your code
Your code has a couple of typos and incorrect attribute names that'll break things even after fixing the NameError:
(1) Wrong attribute in the node class
Your node class was defining a head attribute, but nodes should have a next pointer to link to the next node. Fix that:
class node: def __init__(self, data): self.data = data self.next = None # Replace head with next
(2) Undefined listnode in mergelist
You used dummy=listnode(0) in your merge method, but your node class is named node. Swap that out:
def mergelist(self, l1, l2): dummy = node(0) # Use your actual node class pointer = dummy while l1 and l2: if l1.data < l2.data: pointer.next = l1 l1 = l1.next else: pointer.next = l2 l2 = l2.next pointer = pointer.next # Attach any remaining nodes from either list pointer.next = l1 if l1 else l2 return dummy.next
(3) Push method works once node is fixed
Your push method was written correctly, but it relied on the node class having a next attribute—now that we fixed the node class, this method will work as intended.
3. Full working code example
Here's the complete, runnable version of your code with all fixes applied:
class node: def __init__(self, data): self.data = data self.next = None class linkedlist: def __init__(self): self.head = None def push(self, newdata): newnode = node(newdata) newnode.next = self.head self.head = newnode def mergelist(self, l1, l2): dummy = node(0) pointer = dummy while l1 and l2: if l1.data < l2.data: pointer.next = l1 l1 = l1.next else: pointer.next = l2 l2 = l2.next pointer = pointer.next pointer.next = l1 if l1 else l2 return dummy.next # Test the code ll1 = linkedlist() ll1.push(3) ll1.push(2) ll1.push(1) ll2 = linkedlist() ll2.push(6) ll2.push(5) ll2.push(4) merged_list = linkedlist() merged_list.head = merged_list.mergelist(ll1.head, ll2.head) # Print the merged list current = merged_list.head while current: print(current.data, end=" ") current = current.next # Output: 1 2 3 4 5 6
内容的提问来源于stack exchange,提问作者Chetan P

