Performance Issues with Merge Sort on Linked Lists in Python 3.9 - How to Optimize?
I'm trying to implement I'm working on a personal project and Hey everyone, I'm running into an issue that's driving me crazy... I'm currently implementing a merge sort algorithm to sort a linked list in Python 3.9, but I've encountered significant performance issues when dealing with larger datasets. My implementation is straightforward, but the time complexity seems to be worse than expected. Here's the code I have so far: ```python class ListNode: def __init__(self, value=0, next=None): self.value = value self.next = next class LinkedList: def __init__(self): self.head = None def merge_sort(self, head): if not head or not head.next: return head mid = self.get_middle(head) left = self.merge_sort(head) right = self.merge_sort(mid) return self.merge(left, right) def get_middle(self, head): if not head: return head slow = head fast = head while fast.next and fast.next.next: slow = slow.next fast = fast.next.next mid = slow.next slow.next = None # Split the list return mid def merge(self, left, right): if not left: return right if not right: return left if left.value <= right.value: result = left result.next = self.merge(left.next, right) else: result = right result.next = self.merge(left, right.next) return result ``` When sorting a linked list of around 100,000 nodes, the execution time exceeds 10 seconds, which seems excessive. I've tried optimizing the `merge` function, but the recursive calls seem to be piling up, leading to increased overhead. I'm getting a recursion limit error when sorting even larger lists, which makes me think I need a more iterative approach. I've also considered converting the linked list to an array, sorting it, and converting it back, but I'm concerned about the extra space complexity. Is there a more efficient way to implement merge sort for linked lists in Python 3.9? Are there specific patterns or best practices I should be following to avoid the performance hit? Any advice would be greatly appreciated! My development environment is Linux. This is part of a larger CLI tool I'm building. How would you solve this? Thanks in advance! I recently upgraded to Python 3.10. Thanks for your help in advance!