fucking-algorithm icon indicating copy to clipboard operation
fucking-algorithm copied to clipboard

[bug][python] vvXgSW

Open labuladong opened this issue 1 year ago • 1 comments

此 issue 的目的是修复 chatGPT 转译的多语言代码,更详细的背景信息和修复流程见:https://github.com/labuladong/fucking-algorithm/issues/1113

请在提交 bug 之前先搜索

  • [X] 我已经搜索过 issues,没有发现相同的 bug。

出错的题目链接

https://leetcode.cn/problems/vvXgSW/

报错信息

Line 13: TypeError: '<' not supported between instances of 'ListNode' and 'ListNode'
TypeError: '<' not supported between instances of 'ListNode' and 'ListNode'
    heapq.heappush(pq, (head.val, head))
Line 13 in mergeKLists (Solution.py)
    ret = Solution().mergeKLists(param_1)
Line 43 in _driver (Solution.py)
    _driver()
Line 54 in <module> (Solution.py)

你是否愿意提交 PR 修复这个 bug?

  • [ ] 我愿意!

labuladong avatar Mar 26 '23 09:03 labuladong

''' 分治法: 只有一个链表时返回该链表 只有两个链表时返回合并后的链表 有两个以上链表时,拆分成两部分分别合并 ''' class Solution: def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: head = ListNode(None) p = head while l1 and l2: if l1.val <= l2.val: p.next = l1 l1 = l1.next else: p.next = l2 l2 = l2.next p = p.next if not l1: p.next = l2 else: p.next = l1 return head.next def mergeKLists(self, lists: List[ListNode]) -> ListNode: n = len(lists) if n == 0: return None return self.mergeLR(lists,0,n-1) def mergeLR(self,lists,L,R): if R == L: return lists[L] if R-L != 1: mid = (L+R)//2 lists[L] = self.mergeLR(lists,L,mid) lists[R] = self.mergeLR(lists,mid+1,R) return self.mergeTwoLists(lists[L],lists[R])

sucst avatar Apr 16 '23 10:04 sucst