【发布时间】:2022-01-06 20:18:54
【问题描述】:
上下文:我试图在 vscode 中模拟 leetcode 的测试用例,因为 leetcode 不提供免费用户可用的调试器。 https://leetcode.com/problems/merge-two-sorted-lists/
这是讨论选项卡中的代码解决方案:
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def add_nodes(self, parent, vals):
while len(vals) > 0:
node = ListNode(vals[0])
parent.next = node
return self.add_nodes(node, vals[1:])
class Solution:
def mergeTwoLists(self, list1: ListNode, list2: ListNode) -> ListNode:
l1 = []
while list1 != None:
l1.append(list1.val)
list1 = list1.next
while list2 != None:
l1.append(list2.val)
list2 = list2.next
l1.sort()
if l1 == []:
return None
new_list = ListNode(l1[0])
new_list.add_nodes(new_list, l1[1:])
return new_list
# The code below is what I used inside vscode to simulate leetcode's test case
program = Solution()
list = program.mergeTwoLists(ListNode([1, 3, 4]), ListNode([1, 2, 4]))
print(list.__dict__)
我要完成的工作:我想在 solution 类中打印返回的对象。
我预期会发生什么: 我希望 2 个链表 [1,2,4] [1,3,4] 被合并,打印输出为 [1,1 ,2,3,4,4]
问题:但是,这是我在研究中使用的唯一方法,但它仍然不起作用print(list.__dict__)。打印输出为
{'val': [1, 2, 4], 'next': <__main__.ListNode object at 0x0000024D48203460>}
【问题讨论】:
-
您正在打印对象。
标签: python python-3.x algorithm sorting