этот код дает NameError, показывая, что я не определил функцию, но на самом деле я определил функцию в классе.
Я много пробовал, но не смог ее разрешить.
class Node(object):
def __init__(self, data):
self.data = data
self.next = None
class LinkedList(object):
def __init__(self):
self.head = None
def printList(self):
temp = self.head
while temp:
print(temp.data)
temp = temp.next
def append(self, new_data):
new_node = Node(new_data)
if self.head is None:
self.head = new_node
return
last = self.head
while last.next:
last = last.next
last.next = new_node
def merge(self, head1, head2):
temp = None
if head1 is None:
return head2
if head2 is None:
return head1
if head1.data <= head2.data:
temp = head1
temp.next = merge(head1.next, head2)
else:
temp = head2
temp.next = merge(head1, head2.next)
return temp
list1 = LinkedList()
list1.append(10)
list1.append(20)
list1.append(30)
list1.append(40)
list1.append(50)
list2 = LinkedList()
list2.append(5)
list2.append(15)
list2.append(18)
list2.append(35)
list2.append(60)
list3 = LinkedList()
list3.head = merge(list1.head, list2.head)
print("Merged Linked List is : ")
list3.printList()
NameError Traceback (most recent call last)
<ipython-input-11-22fca0a2d24d> in <module>()
57
58 list3 = LinkedList()
---> 59 list3.head = merge(list1.head, list2.head)
60
61 print("Merged Linked List is : ")
NameError: name 'merge' is not defined