本文整理匯總了Python中LinkedList.LinkedList.head方法的典型用法代碼示例。如果您正苦於以下問題:Python LinkedList.head方法的具體用法?Python LinkedList.head怎麽用?Python LinkedList.head使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類LinkedList.LinkedList
的用法示例。
在下文中一共展示了LinkedList.head方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_find_loop_node
# 需要導入模塊: from LinkedList import LinkedList [as 別名]
# 或者: from LinkedList.LinkedList import head [as 別名]
def test_find_loop_node(self):
n1 = Node(1)
n2 = Node(2)
n3 = Node(3)
n4 = Node(4)
n5 = Node(5)
n6 = Node(6)
n7 = Node(7)
n8 = Node(8)
n9 = Node(9)
n10 = Node(10)
n11 = Node(11)
l1 = LinkedList()
l1.head = n1
n1.next = n2
n2.next = n3
n3.next = n4
n4.next = n5
n5.next = n6
n6.next = n7
n7.next = n8
n8.next = n9
n9.next = n10
n10.next = n11
n11.next = n6
actual = find_loop_node(l1)
self.assertEqual(actual, n6)
l1 = LinkedList()
l1.head = n1
n1.next = n2
n2.next = n3
n3.next = n4
n4.next = n5
n5.next = n6
n6.next = n7
n7.next = n8
n8.next = n9
n9.next = n10
n10.next = n11
n11.next = n8
actual = find_loop_node(l1)
self.assertEqual(actual, n8)
示例2: find_intersection
# 需要導入模塊: from LinkedList import LinkedList [as 別名]
# 或者: from LinkedList.LinkedList import head [as 別名]
def find_intersection(lst1, lst2):
current_lst1 = lst1.head
current_lst2 = lst2.head
count_lst1 = 1
count_lst2 = 1
while current_lst1.get_next():
count_lst1 += 1
current_lst1 = current_lst1.get_next()
while current_lst2.get_next():
count_lst2 += 1
current_lst2 = current_lst2.get_next()
if not current_lst1 == current_lst2:
return None
else:
diff = lst1.size() - lst2.size()
if diff > 0:
pt = lst1.head
for i in range(diff):
pt = pt.get_next()
lst1.head = pt
elif diff < 0:
pt = lst2.head
for i in range(abs(diff)):
pt = pt.get_next()
lst2.head = pt
current_lst1 = lst1.head
current_lst2 = lst2.head
while not current_lst1 == current_lst2:
current_lst1 = current_lst1.get_next()
current_lst2 = current_lst2.get_next()
return_lst = LinkedList()
return_lst.head = current_lst1
return return_lst