本文整理汇总了Python中linkedlist.LinkedList.create_from_array方法的典型用法代码示例。如果您正苦于以下问题:Python LinkedList.create_from_array方法的具体用法?Python LinkedList.create_from_array怎么用?Python LinkedList.create_from_array使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类linkedlist.LinkedList
的用法示例。
在下文中一共展示了LinkedList.create_from_array方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: xrange
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import create_from_array [as 别名]
node = ll.head
for i in xrange(index - 1):
node = node.next
return node
def size(ll):
ans = 0
node = ll.head
while node is not None:
ans += 1
node = node.next
return ans
def kth_without_size(ll, k):
node = ll.head
runner = ll.head
# Why this + 1?
for i in xrange(k + 1):
runner = runner.next
while runner is not None:
node = node.next
runner = runner.next
return node
l = LinkedList.create_from_array([1, 2, 3, 4, 5, 6, 7])
print kth_to_last(l, 3)
print kth_without_size(l, 0)
示例2: remove_dups_lean
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import create_from_array [as 别名]
while node is not None:
if node.value in seen:
previous.next = node.next
else:
seen.add(node.value)
previous = previous.next
node = node.next
return ll
def remove_dups_lean(ll):
first = ll.head
while first is not None:
second = first.next
previous = first
while second is not None:
if first.value == second.value:
previous.next = second.next
else:
previous = previous.next
second = second.next
first = first.next
return ll
l = LinkedList.create_from_array([2, 3, 1, 1, 3, 4, 5, 6, 7])
remove_dups(l).print_list()
l2 = LinkedList.create_from_array([2, 3, 1, 1, 3, 4, 5, 6, 7])
remove_dups_lean(l2).print_list()
示例3: partition
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import create_from_array [as 别名]
from linkedlist import LinkedList
def partition(ll, k):
larger_list = LinkedList()
previous = ll.head
node = previous.next
while node is not None:
if node.value >= k:
previous.next = node.next
larger_list.add(node.value)
else:
previous = previous.next
node = node.next
previous.next = larger_list.head
return ll
l = LinkedList.create_from_array([3, 5, 8, 5, 10, 2, 1])
partition(l, 5).print_list()
示例4: is_palindrome2
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import create_from_array [as 别名]
return True
def is_palindrome2(ll):
nl = LinkedList()
size = ll.length/2
node = ll.head
for i in range(size):
nl.add(node.value)
node = node.next
node2 = nl.head
if ll.length % 2 == 1:
node = node.next
while node:
if node.value != node2.value:
return False
node = node.next
node2 = node2.next
return True
l = LinkedList.create_from_array([1, 2, 3, 4, 5, 6, 7])
t = LinkedList.create_from_array(list('abacacaba'))
assert is_palindrome(l) is False
assert is_palindrome(t) is True
assert is_palindrome2(l) is False
assert is_palindrome2(t) is True
示例5: xrange
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import create_from_array [as 别名]
key=lambda x: x.length)
for i in xrange(longer.length - shorter.length):
shorter.add(0)
result, carry = sum_lists_forward_aux(shorter.head, longer.head)
if carry > 0:
result.add(carry)
return result
def sum_lists_forward_aux(a, b):
"""
Example Input: 4 -> 3 -> 2
9 -> 8 -> 7
Example Output: 4 -> 1 -> 9, 1
"""
if a is None:
return LinkedList(), 0
tmp, carry = sum_lists_forward_aux(a.next, b.next)
value = a.value + b.value + carry
tmp.add(value % 10)
return tmp, value/10
l = LinkedList.create_from_array([5, 4, 1, 1])
t = LinkedList.create_from_array([2, 8, 9])
sum_lists(l, t).print_list()
sum_lists_forward(l, t).print_list()