當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。