简体   繁体   English

在python中使用插入排序对单链表进行排序

[英]Sorting singly linked list using insertion sort in python

I'm new to python coming form c++ i don't know how to work with linked list without pointers, that being said I've written this code but it returns the same list without sorting it at all我是来自 c++ 的 python 新手,我不知道如何在没有指针的情况下使用链表,据说我已经编写了这段代码,但它返回了相同的列表,根本没有对其进行排序

class ListNode:
    def __init__(self, val=0, next=None):
          self.val = val
          self.next = next
def insertionSortList(head):
    if head.next==None:
        return head
    #checkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkks
    loop=head
    i=head
    while(i.next!=None):
        save_val=i.next
        i=i.next.next
        while True:
            if loop!=i and save_val.val>loop.val:
                loop=loop.next
            else:
                if loop==head:
                    save_val=loop
                    break
                else:
                    save_val=loop.next
                    loop=save_val
                    loop=head
                    break
    return head

You are not incrementing the loop node variable.您没有增加循环节点变量。 Your code breaks out of the inner while loop before the loop node variable gets a chance to increment loop=loop.next .您的代码在循环节点变量有机会增加loop=loop.next之前跳出内部 while 循环。 Here is my solution, I separated the Node and Linked List classes.这是我的解决方案,我将 Node 和 Linked List 类分开。 Setup a length variable and incremented the value each time I inserted a node.每次插入节点时,设置一个长度变量并增加该值。 Then used the normal insertion sort method.然后使用正常的插入排序方法。

class Node:
  
    def __init__(self,value):
        self.value = value
        self.next = None
        
class ListNode:
    
    def __init__(self):
        self.head = None
        self.length = 0
        
    def __repr__(self):
        '''Allows user to print values in the ListNode'''  
        
        values = []
        current = self.head
        while current:
            values.append(current.value)
            current = current.next
        return ', '.join(str(value) for value in values)
    
    def insert(self,value):
        '''Inserts nodes into the ListNode class'''
        
        newNode = Node(value)
        if self.head is None:
            self.head = newNode
        else:
            newNode.next = self.head
            self.head = newNode
        self.length += 1
        return self
    
    def insertionSortList(self):
        '''Insertion sort method: held a temp variable and inserted the smallest value from temp through the rest of the list then incremented temp variable to the next idx/node'''
        
        current = self.head
        for _ in range(self.length-1):
            tempNode = current.next
            while tempNode:
                if tempNode.value < current.value:
                    current.value, tempNode.value = tempNode.value, current.value
                tempNode = tempNode.next
            current = current.next
        return self              

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM