当前位置: 首页>>代码示例>>Python>>正文


Python LinkedList.head方法代码示例

本文整理汇总了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)
开发者ID:vivekkeshore,项目名称:ctci,代码行数:46,代码来源:list_loop_2_6_test.py

示例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
开发者ID:ZachLiuGIS,项目名称:Algorithm-Enthusiasts,代码行数:46,代码来源:find_intersection.py


注:本文中的LinkedList.LinkedList.head方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。