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


Python LinkedList.generate方法代码示例

本文整理汇总了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))
开发者ID:roachsinai,项目名称:Coding_Interviews,代码行数:23,代码来源:2_Return_Kth_To_Last.py

示例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))
开发者ID:5imp1e,项目名称:CtCI-6th-Edition-Python,代码行数:32,代码来源:5_Sum_Lists.py

示例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)
开发者ID:5imp1e,项目名称:CtCI-6th-Edition-Python,代码行数:31,代码来源:1_Remove_Dups.py

示例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)



开发者ID:alothings,项目名称:cracking_the_coding_interview,代码行数:29,代码来源:remove_dups.py


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