本文整理汇总了Python中LinkedList.LinkedList.next方法的典型用法代码示例。如果您正苦于以下问题:Python LinkedList.next方法的具体用法?Python LinkedList.next怎么用?Python LinkedList.next使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类LinkedList.LinkedList
的用法示例。
在下文中一共展示了LinkedList.next方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_link
# 需要导入模块: from LinkedList import LinkedList [as 别名]
# 或者: from LinkedList.LinkedList import next [as 别名]
def add_link(head1, head2):
carry = 0
dummy1 = LinkedList()
dummy2 = LinkedList()
dummy1.next = head1
dummy2.next = head2
p1 = dummy1
p2 = dummy2
while p1.next and p2.next:
carry += p1.next.val + p2.next.val
p1.next.val = carry % 10
carry /= 10
p1 = p1.next
p2 = p2.next
while p1.next:
carry += p1.next.val
p1.next.val = carry % 10
carry /= 10
p1 = p1.next
while p2.next:
carry += p2.next.val
p1.next = LinkedList(carry % 10)
carry /= 10
p1 = p1.next
p2 = p2.next
if carry:
p1.next = LinkedList(carry)
return dummy1.next
示例2: reverse_list_test
# 需要导入模块: from LinkedList import LinkedList [as 别名]
# 或者: from LinkedList.LinkedList import next [as 别名]
def reverse_list_test(self):
"""Test the the linked list is correctly able to reverse the order of items within itself. Asserts that the list
has the correct ordering of items after being reversed AND after being reversed a second time.
"""
items = range(1, 10) # test list of items ( numbers 1 - 10)
linked_list = LinkedList()
for item in items: # add each test item to linked list
linked_list.add(item)
linked_list.reverse() # reverse order of both lists
items = reversed(items)
for ll_item, item in zip(linked_list.next(), items): # check that each index holds same item
self.assertEqual(ll_item, item)
linked_list.reverse() # reverse linked list again
for ll_item, item in zip(linked_list.next(), range(1, 10)): # check that each index holds same item
self.assertEqual(ll_item, item)
示例3: linked_list_test
# 需要导入模块: from LinkedList import LinkedList [as 别名]
# 或者: from LinkedList.LinkedList import next [as 别名]
def linked_list_test(self):
"""Test that the singly linked list is able to correctly append items. Asserts that the list contains all items
AND that the items are int he correct order.
"""
items = range(-10, 10) # randomly ordered list
shuffle(items)
linked_list = LinkedList() # linked list containing same items in same order
for item in items:
linked_list.add(item)
ll_items = [x for x in linked_list.next()] # contents of linked list as a python list
self.assertEqual(ll_items, items) # test that the contents and order are the same as the original list
示例4: remove
# 需要导入模块: from LinkedList import LinkedList [as 别名]
# 或者: from LinkedList.LinkedList import next [as 别名]
from LinkedList import LinkedList
def remove(node):
if node:
if node.next:
node.val = node.next.val
node.next = node.next.next
else:
pass # cannot handle using this method
if __name__ == '__main__':
n1 = LinkedList(1)
n2 = LinkedList(2)
n3 = LinkedList(3)
n1.next = n2
n2.next = n3
remove(n3)
n1.print_self()
示例5: LinkedList
# 需要导入模块: from LinkedList import LinkedList [as 别名]
# 或者: from LinkedList.LinkedList import next [as 别名]
carry /= 10
p1 = p1.next
p2 = p2.next
while p1.next:
carry += p1.next.val
p1.next.val = carry % 10
carry /= 10
p1 = p1.next
while p2.next:
carry += p2.next.val
p1.next = LinkedList(carry % 10)
carry /= 10
p1 = p1.next
p2 = p2.next
if carry:
p1.next = LinkedList(carry)
return dummy1.next
if __name__ == '__main__':
p11 = LinkedList(8)
p21 = LinkedList(9)
p22 = LinkedList(9)
p23 = LinkedList(9)
p21.next = p22
p22.next = p23
add_link(p11, p21).print_self()