Python 在插入到链接列表时管理重复项

Python 在插入到链接列表时管理重复项,python,recursion,linked-list,singly-linked-list,Python,Recursion,Linked List,Singly Linked List,我编写了一个算法,用于将课程对象插入按其编号成员变量排序的链接列表中。它工作得很好,除非我尝试插入重复项,例如2420的两个实例。列表\uuuu str\uuuu输出当前如下所示: cs1400 Introduction to Programming Grade:3.6 Credit Hours: 4 cs1410 C++ Programming Grade:2.6 Credit Hours: 4 cs2420 Introduction to Data Structures Grade:3.

我编写了一个算法,用于将课程对象插入按其
编号
成员变量排序的链接列表中。它工作得很好,除非我尝试插入重复项,例如2420的两个实例。列表
\uuuu str\uuuu
输出当前如下所示:

cs1400 Introduction to Programming Grade:3.6 Credit Hours: 4 
cs1410 C++ Programming Grade:2.6 Credit Hours: 4 
cs2420 Introduction to Data Structures Grade:3.2 Credit Hours: 4 
cs2810 Computer Architecture Grade:3.8 Credit Hours: 3 
这是我的insert方法代码

def insert(self, course=None):
        """Insert the specified Course in Course Number ascending order.""" 
        def insert_helper(cursor, course):

            if course is None:
                return

            if course.number <= self.head.number: # start of the list
                self.head = course
                return

            if cursor.next is None or course.number <= cursor.next.number: # 
                course.next = cursor.next
                cursor.next = course
                return

            insert_helper(cursor.next, course)


        if self.head is None:
            self.head = course
            return
        cursor = self.head
        insert_helper(cursor, course)
def插入(self,course=None):
“”“按课程编号升序插入指定的课程。”“”
def insert_辅助对象(光标,路线):
如果课程没有:
返回

如果course.number我不认为重复的课程编号本身就是问题所在

您的程序中有一个错误:

            if course.number <= self.head.number: # start of the list
                self.head = course
                return
您需要设置
course.next
到两个位置的某个位置


另外请注意,如果
course.number,那么当您尝试插入一个重复的课程号时,它会出错,您希望它做什么?因此,您需要添加一个案例,说明当course.number==course.next时,您可以解释重复的课程号,显然,你必须知道如何处理重复的课程号…@DavidK我希望它作为列表中的另一个条目插入。对于这样的问题,在程序运行的情况下张贴输入和输出的示例,在程序不运行的情况下张贴输入和输出的示例会有所帮助。我怀疑您只是没有尝试足够多的不同测试数据来正确诊断错误。请描述当前输入/输出和所需输出,并共享一个示例。
                course.next = cursor.next