Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

linked list - runtime error in bubble sort (iterative) python

i am getting the following run time error...pls help and that too for only 2 test cases rest are workin fine... the error is

Traceback (most recent call last): File solution.py , line 82, in <module> head = bubbleSort(head) File solution.py , line 43, in bubbleSort sortedlist = sortedMerge(left, right) File solution.py , line 32, in sortedMerge result.next = sortedMerge(a, b.next) File solution.py , line 29, in sortedMerge result.next = sortedMerge(a.next, b) File solution.py , line 29, in sortedMerge result.next = sortedMerge(a.next, b) File solution.py , line 29, in sortedMerge result.next = sortedMerge(a.next, b) File solution.py , line 32, in sortedMerge result.next = sortedMerge(a, b.next)

line nos. may not match

from sys import stdin

    #Following is the Node class already written for the Linked List
    class Node :
        def __init__(self, data) :
            self.data = data
            self.next = None
    
    
    
    def getMiddle(head):
        if (head == None):
            return head
        slow = head
        fast = head
        while (fast.next != None and fast.next.next != None):
            slow = slow.next
            fast = fast.next.next
        return slow
    
    def sortedMerge(a, b):
        result = None
        if a == None:
            return b
        if b == None:
            return a
        if a.data <= b.data:
            result = a
            result.next = sortedMerge(a.next, b)
        else:
            result = b
            result.next = sortedMerge(a, b.next)
        return result
    
    def bubbleSort(head):
        if head == None or head.next == None:
            return head
        middle = getMiddle(head)
        nexttomiddle = middle.next
        middle.next = None
        left = bubbleSort(head)
        right = bubbleSort(nexttomiddle)
        sortedlist = sortedMerge(left, right)
        return sortedlist
    
    def takeInput() :
        head = None
        tail = None
    
        datas = list(map(int, stdin.readline().rstrip().split(" ")))
    
        i = 0
        while (i < len(datas)) and (datas[i] != -1) :
            data = datas[i]
            newNode = Node(data)
    
            if head is None :
                head = newNode
                tail = newNode
    
            else :
                tail.next = newNode
                tail = newNode
    
            i += 1
    
        return head
    
    
    
    def printLinkedList(head) :
    
        while head is not None :
            print(head.data, end = " ")
            head = head.next
    
        print()
    
    
    #main
    head = takeInput()
    head = bubbleSort(head)
    printLinkedList(head)
question from:https://stackoverflow.com/questions/65855909/runtime-error-in-bubble-sort-iterative-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...