當前位置: 首頁>>代碼示例>>Python>>正文


Python LinkedList.next方法代碼示例

本文整理匯總了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
開發者ID:aihex,項目名稱:cracking-the-coding-interview-python,代碼行數:32,代碼來源:Q2.4.py

示例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)
開發者ID:Clarksj4,項目名稱:Algorithms_and_Data_Structures,代碼行數:20,代碼來源:test.py

示例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
開發者ID:Clarksj4,項目名稱:Algorithms_and_Data_Structures,代碼行數:15,代碼來源:test.py

示例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()
開發者ID:aihex,項目名稱:cracking-the-coding-interview-python,代碼行數:24,代碼來源:Q2.3.py

示例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()
開發者ID:aihex,項目名稱:cracking-the-coding-interview-python,代碼行數:32,代碼來源:Q2.4.py


注:本文中的LinkedList.LinkedList.next方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。