本文整理汇总了Python中LinkedList.LinkedList.generate方法的典型用法代码示例。如果您正苦于以下问题:Python LinkedList.generate方法的具体用法?Python LinkedList.generate怎么用?Python LinkedList.generate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类LinkedList.LinkedList
的用法示例。
在下文中一共展示了LinkedList.generate方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: O
# 需要导入模块: from LinkedList import LinkedList [as 别名]
# 或者: from LinkedList.LinkedList import generate [as 别名]
# O(N)
from LinkedList import LinkedList
def kth_to_last(ll, k):
runner = current = ll.head
for _ in range(k):
if runner is None:
return
runner = runner.next
while runner:
current = current.next
runner = runner.next
return current
ll = LinkedList()
ll.generate(10, 0, 99)
print(ll)
print(kth_to_last(ll, 3))
示例2: len
# 需要导入模块: from LinkedList import LinkedList [as 别名]
# 或者: from LinkedList.LinkedList import generate [as 别名]
if len(ll_a) < len(ll_b):
for i in range(len(ll_b) - len(ll_a)):
ll_a.add_to_beginning(0)
else:
for i in range(len(ll_a) - len(ll_b)):
ll_b.add_to_beginning(0)
# Find sum
n1, n2 = ll_a.head, ll_b.head
result = 0
while n1 and n2:
result = (result * 10) + n1.value + n2.value
n1 = n1.next
n2 = n2.next
# Create new linked list
ll = LinkedList()
ll.add_multiple([int(i) for i in str(result)])
return ll
ll_a = LinkedList()
ll_a.generate(4, 0, 9)
ll_b = LinkedList()
ll_b.generate(3, 0, 9)
print(ll_a)
print(ll_b)
print(sum_lists(ll_a, ll_b))
print(sum_lists_followup(ll_a, ll_b))
示例3: remove_dups_followup
# 需要导入模块: from LinkedList import LinkedList [as 别名]
# 或者: from LinkedList.LinkedList import generate [as 别名]
return ll
def remove_dups_followup(ll):
if ll.head is None:
return
current = ll.head
while current:
runner = current
while runner.next:
if runner.next.value == current.value:
runner.next = runner.next.next
else:
runner = runner.next
current = current.next
return ll.head
ll = LinkedList()
ll.generate(100, 0, 9)
print(ll)
remove_dups(ll)
print(ll)
ll.generate(100, 0, 9)
print(ll)
remove_dups_followup(ll)
print(ll)
示例4: remove_dups_runner
# 需要导入模块: from LinkedList import LinkedList [as 别名]
# 或者: from LinkedList.LinkedList import generate [as 别名]
hash_set.add(current.next.value)
current = current.next
return ll
def remove_dups_runner(ll):
current = ll.head
while current:
runner = current
while runner.next:
if current.value is not runner.next.value:
runner = runner.value
else:
runner.next = runner.next.next
return ll
ll = LinkedList()
ll.generate(100, 0, 6)
print(ll)
remove_dups(ll)
print("Result: ", ll)
ll2 = LinkedList()
ll2.generate(100, 0, 6)
print(ll2)
remove_dups(ll2)
print("Result: ", ll2)