本文整理匯總了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)