我正在尝试定义一个可以从链接的整数列表中获取最小值的函数。
Given Function(not allowed to be modified):
class LN:
def __init__(self,value,next=None):
self.value = value
self.next = next
def list_to_ll(l):
if l == []:
return None
front = rear = LN(l[0])
for v in l[1:]:
rear.next = LN(v)
rear = rear.next
return front
函数list_to_ll将普通列表转换为链表:
A recursive function I am trying to define:
def get_min(ll):
if ll == None:
return None
else:
if ll.value < ll.next.value:
return ll.value
return get_min(ll.next)
例如:
get_min(list_to_ll([7, 3, 5, 2, 0]))--> 0
但我的功能给了我:
RuntimeError: maximum recursion depth exceeded in comparison
请帮忙。实际的代码将非常感激。
答案 0 :(得分:5)
为您的数据结构实施__iter__
,以便您可以对其进行迭代。然后,您可以使用常规的min()
和max()
函数(以及for
循环,any()
和all()
函数,map()
和列表理解......等等。
def __iter__(self):
ptr = self
while ptr is not None:
yield ptr.value
ptr = ptr.next
答案 1 :(得分:2)
您的get_min
函数包含以下错误:
基本情况应为if ll.next == None
而不是if ll == None
。实际上,空列表的最小值没有明确定义。但如果ll.next
为None
,则表示您的列表中只包含一个项目。在这种情况下,列表的最小值是项目本身,即ll.value
当列表包含多个元素时,可以通过将列表的第一个元素(ll.value
)与剩余列表的最小值进行比较来获得列表的最小值
ll.next
开始(列表的尾部)。
最后,最好使用is
运算符来测试Python变量是否为None
。
工作代码可能如下:
def get_min(ll):
if ll.next is None:
return ll.value
else:
tail_min = get_min(ll.next)
if ll.value < tail_min:
return ll.value
else:
return tail_min
如果您可以使用min
功能比较两个数字,则更简洁的版本是:
def get_min(ll):
if ll.next is None:
return ll.value
else:
return min(ll.value, get_min(ll.next))
最后,当列表为空时,您可以引发异常,以警告用户在不适用的情况下使用该函数:
def get_min(ll):
if ll is None:
raise ValueError("Cannot compute the minimum of the empty list.")
elif ll.next is None:
return ll.value
else:
return min(ll.value, get_min(ll.next))
答案 2 :(得分:0)
我已经在python中做到了。 首先,我们创建了一个循环链接列表,然后我们 打印minNode()值和maxNode()值。
#Represents the node of list.
class Node:
def __init__(self,data):
self.data = data;
self.next = None;
class CreateList:
#Declaring head and tail pointer as null.
def __init__(self):
self.head = Node(None);
self.tail = Node(None);
self.head.next = self.tail;
self.tail.next = self.head;
#This function will add the new node at the end of the list.
def add(self,data):
newNode = Node(data);
#Checks if the list is empty.
if self.head.data is None:
#If list is empty, both head and tail would point to new node.
self.head = newNode;
self.tail = newNode;
newNode.next = self.head;
else:
#tail will point to new node.
self.tail.next = newNode;
#New node will become new tail.
self.tail = newNode;
#Since, it is circular linked list tail will point to head.
self.tail.next = self.head;
#Finds out the minimum value node in the list
def minNode(self):
current = self.head;
#Initializing min to initial node data
minimum = self.head.data;
if(self.head == None):
print("List is empty");
else:
while(True):
#If current node's data is smaller than min
#Then replace value of min with current node's data
if(minimum > current.data):
minimum = current.data;
current= current.next;
if(current == self.head):
break;
print("Minimum value node in the list: "+ str(minimum));
#Finds out the maximum value node in the list
def maxNode(self):
current = self.head;
#Initializing max to initial node data
maximum = self.head.data;
if(self.head == None):
print("List is empty");
else:
while(True):
#If current node's data is greater than max
#Then replace value of max with current node's data
if(maximum < current.data):
maximum = current.data;
current= current.next;
if(current == self.head):
break;
print("Maximum value node in the list: "+ str(maximum));
class CircularLinkedList:
cl = CreateList();
#Adds data to the list
cl.add(5);
cl.add(20);
cl.add(10);
cl.add(1);
#Prints the minimum value node in the list
cl.minNode();
#Prints the maximum value node in the list
cl.maxNode();