Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug][python] vvXgSW #1359

Open
1 of 2 tasks
labuladong opened this issue Mar 26, 2023 · 1 comment
Open
1 of 2 tasks

[bug][python] vvXgSW #1359

labuladong opened this issue Mar 26, 2023 · 1 comment
Labels
help wanted Extra attention is needed

Comments

@labuladong
Copy link
Owner

此 issue 的目的是修复 chatGPT 转译的多语言代码,更详细的背景信息和修复流程见:#1113

请在提交 bug 之前先搜索

  • 我已经搜索过 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 labuladong added the help wanted Extra attention is needed label Mar 26, 2023
@sucst
Copy link

sucst commented Apr 16, 2023

'''
分治法:
只有一个链表时返回该链表
只有两个链表时返回合并后的链表
有两个以上链表时,拆分成两部分分别合并
'''
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])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants