如何将值填充到 class in python
How do you populate values into a class in python
我有一个代码可以合并 leetcode 的 2 个链表。但是,经过测试,我遇到了瓶颈。如何用列表填充 ListNode?以下输出只是 1,而不是合并的链表。
from typing import Optional
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def __str__(self):
return str(self.val)
class Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
place_holder = ListNode()
tmp = place_holder
while list1 and list2:
if list1.val < list2.val:
tmp.next = list1
list1 = list1.next
else:
tmp.next = list2
list2 = list2.next
tmp = tmp.next
if list1 is None:
tmp.next = list2
if list2 is None:
tmp.next = list1
return place_holder.next
#input the two integer lists
l1 = [1, 2, 4]
l2 = [1, 3, 4]
list1 = ListNode(l1[0])
list2 = ListNode(l2[0])
list_result = Solution().mergeTwoLists(list1, list2)
print(list_result)
修复你的 __str__
以便打印整个链表:
def __str__(self):
s = f"({str(self.val)})"
if self.next:
s += f" -> {self.next}" # be careful not to build any circular lists...
return s
现在您可以看到合并函数的实际结果:
(1) -> (1)
这是 100% 正确的(至少对于这些输入而言),因为您合并了列表 (1)
和 (1)
。
编写一个函数,让您可以将多个数字的列表转换为单个链表,这样可以更轻松地使用更长的输入来测试此逻辑。一种选择是在 ListNode
构造函数中执行此操作:
from typing import List, Optional, Union
class ListNode:
def __init__(self, val: int = 0, *vals: int) -> None:
self.val = val
self.next = ListNode(*vals) if vals else None
def __str__(self) -> str:
s = f"({str(self.val)})"
if self.next:
s += f" -> {self.next}"
return s
现在你可以做:
list1 = ListNode(1, 2, 4)
list2 = ListNode(1, 3, 4)
list_result = Solution().mergeTwoLists(list1, list2)
print(list_result) # (1) -> (1) -> (2) -> (3) -> (4) -> (4)
您需要一种将常规 Python 列表转换为链表的方法。你的节点每个都用一个整数初始化(不要介意它来自 l1 和 l2),所以它们不能自己增长任何下一个元素。要修复它,请向接受列表的 ListNode 添加一个静态方法:
class ListNode:
@static
def fromList(lst):
if not lst:
return None
return ListNode(lst[0], next=ListNode.fromList(lst[1:]))
这将递归地将Python列表转换为链表。然后你可以像这样初始化你的链表:
list1 = ListNode.fromList(l1)
list2 = (same)
您需要将 list
值转换为 List
值。 (我建议对节点和包含这些节点的列表使用单独的 类。)
class ListNode:
def __init__(self, val, next=None):
self.val = val
self.next = next
def __str__(self):
return str(self.val)
class List:
def __init__(self, values=None):
# Dummy node that is independent of any actual
# data. You can store the length (or really, any
# metadata you like) in the dummy node.
self.head = ListNode(0)
if values is None:
values = []
for x in values:
self.append(x)
def append(self, value):
...
list1 = List(l1)
list2 = List(l2)
您需要实施 List.append
,可能还需要实施一些其他方法。最好在 List
本身进行所有指针争论,并为 mergeTwoLists
提供方法来构造结果 List
.
我有一个代码可以合并 leetcode 的 2 个链表。但是,经过测试,我遇到了瓶颈。如何用列表填充 ListNode?以下输出只是 1,而不是合并的链表。
from typing import Optional
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def __str__(self):
return str(self.val)
class Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
place_holder = ListNode()
tmp = place_holder
while list1 and list2:
if list1.val < list2.val:
tmp.next = list1
list1 = list1.next
else:
tmp.next = list2
list2 = list2.next
tmp = tmp.next
if list1 is None:
tmp.next = list2
if list2 is None:
tmp.next = list1
return place_holder.next
#input the two integer lists
l1 = [1, 2, 4]
l2 = [1, 3, 4]
list1 = ListNode(l1[0])
list2 = ListNode(l2[0])
list_result = Solution().mergeTwoLists(list1, list2)
print(list_result)
修复你的 __str__
以便打印整个链表:
def __str__(self):
s = f"({str(self.val)})"
if self.next:
s += f" -> {self.next}" # be careful not to build any circular lists...
return s
现在您可以看到合并函数的实际结果:
(1) -> (1)
这是 100% 正确的(至少对于这些输入而言),因为您合并了列表 (1)
和 (1)
。
编写一个函数,让您可以将多个数字的列表转换为单个链表,这样可以更轻松地使用更长的输入来测试此逻辑。一种选择是在 ListNode
构造函数中执行此操作:
from typing import List, Optional, Union
class ListNode:
def __init__(self, val: int = 0, *vals: int) -> None:
self.val = val
self.next = ListNode(*vals) if vals else None
def __str__(self) -> str:
s = f"({str(self.val)})"
if self.next:
s += f" -> {self.next}"
return s
现在你可以做:
list1 = ListNode(1, 2, 4)
list2 = ListNode(1, 3, 4)
list_result = Solution().mergeTwoLists(list1, list2)
print(list_result) # (1) -> (1) -> (2) -> (3) -> (4) -> (4)
您需要一种将常规 Python 列表转换为链表的方法。你的节点每个都用一个整数初始化(不要介意它来自 l1 和 l2),所以它们不能自己增长任何下一个元素。要修复它,请向接受列表的 ListNode 添加一个静态方法:
class ListNode:
@static
def fromList(lst):
if not lst:
return None
return ListNode(lst[0], next=ListNode.fromList(lst[1:]))
这将递归地将Python列表转换为链表。然后你可以像这样初始化你的链表:
list1 = ListNode.fromList(l1)
list2 = (same)
您需要将 list
值转换为 List
值。 (我建议对节点和包含这些节点的列表使用单独的 类。)
class ListNode:
def __init__(self, val, next=None):
self.val = val
self.next = next
def __str__(self):
return str(self.val)
class List:
def __init__(self, values=None):
# Dummy node that is independent of any actual
# data. You can store the length (or really, any
# metadata you like) in the dummy node.
self.head = ListNode(0)
if values is None:
values = []
for x in values:
self.append(x)
def append(self, value):
...
list1 = List(l1)
list2 = List(l2)
您需要实施 List.append
,可能还需要实施一些其他方法。最好在 List
本身进行所有指针争论,并为 mergeTwoLists
提供方法来构造结果 List
.