我正在递归地创建自己的merge_list函数,但无法比较两个单链列表中的值
我的代码以
开头class SList:
class _Node:
def __init__(self, element, next=None):
self._element = element
self._next = next
def element(self):
return self._element
def next(self):
return self._next
def set_element(self, element):
self._element = element
def set_next(self, next):
self._next = next
.
.
.
.
def merge_list(s1, s2): # merge_list recursively
if s1 is None and s2 is None:
return None
if s1 is None:
return s2
if s2 is None:
return s1
p1 = s1._head
p2 = s2._head
if p1 < p2:
s3 = s1
s3._next = merge_list(s1.next(), s2)
else:
s3 = s2
s3._next = merge_list(s1, s2.next())
return s3
我的输入是...
s1 = SList()
s1.insert_first("1")
s1.insert_first("3")
s1.insert_first("5")
s1.insert_first("7")
s1.insert_first("9")
s1.insert_first("11")
s1.insert_first("13")
print(f"s1 :", s1)
s2 = SList()
s2.insert_first("2")
s2.insert_first("4")
s2.insert_first("6")
print(f"s2 : ", s2)
merge_list(s1, s2)
两个列表制作成功,但是当我使用自己的merge_list函数时, 输出是...
s1 : 13 -> 11 -> 9 -> 7 -> 5 -> 3 -> 1 -> None: 7 element(s)
File "###################(my address)", line 312, in <module>
s2 : 6 -> 4 -> 2 -> None: 3 element(s)
merge_list(s1, s2)
File "###################(my address)", line 158, in merge_list
if p1 < p2:
TypeError: '<' not supported between instances of '_Node' and '_Node'
在出现此TypeError消息之后,我无法将列表的值与“ <”进行比较,那么我该怎么做才能比较列表的值?