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


Python ListNode.arrayToList方法代码示例

本文整理汇总了Python中ListNode.ListNode.arrayToList方法的典型用法代码示例。如果您正苦于以下问题:Python ListNode.arrayToList方法的具体用法?Python ListNode.arrayToList怎么用?Python ListNode.arrayToList使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ListNode.ListNode的用法示例。


在下文中一共展示了ListNode.arrayToList方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: ListNode

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import arrayToList [as 别名]
    tail = dummy
    carry = 0
    while l1 is not None and l2 is not None:
        s = l1.val + l2.val + carry
        tail.next = ListNode(s % 10)
        carry = s / 10
        l1 = l1.next
        l2 = l2.next
        tail = tail.next
    while l1 is not None:
        s = l1.val + carry
        tail.next = ListNode(s % 10)
        carry = s / 10
        l1 = l1.next
        tail = tail.next
    while l2 is not None:
        s = l2.next + carry
        tail.next = ListNode(s % 10)
        carry = s / 10
        l2 = l2.next
        tail = tail.next
    if carry != 0:
        tail.next = ListNode(carry)

    return dummy.next


l1 = ListNode.arrayToList([6, 1, 7])
l2 = ListNode.arrayToList([2, 9, 5])
ListNode.printList(addTwoNumbersII(l1, l2))
开发者ID:cutewindy,项目名称:CodingInterview,代码行数:32,代码来源:addTwoNumbersII.py

示例2: mergeTwoSortedLists

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import arrayToList [as 别名]
from ListNode import ListNode

def mergeTwoSortedLists(l1, l2):
    if l1 == None and l2 == None:
        return None
    dummy = ListNode(0)
    tail = dummy
    while l1 != None and l2 != None:
        if l1.val < l2.val:
            tail.next = l1
            l1 = l1.next
        else:
            tail.next = l2
            l2 = l2.next
        tail = tail.next
    if l1 != None:
        tail.next = l1
    else:
        tail.next = l2

    return dummy.next




head1 = ListNode.arrayToList([1, 3, 8, 11, 15])
head2 = ListNode.arrayToList([2])

ListNode.printList(mergeTwoSortedLists(head1, head2))

开发者ID:cutewindy,项目名称:CodingInterview,代码行数:31,代码来源:mergeTwoSortedLists.py

示例3: removeDuplicatesfromUnsortedList

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import arrayToList [as 别名]
# Have you met this question in a real interview? Yes
# Example
# Given 1->3->2->1->4.

# Return 1->3->2->4

# Challenge
# (hard) How would you solve this problem if a temporary buffer is not allowed?
# In this case, you don't need to keep the order of nodes.

from ListNode import ListNode

def removeDuplicatesfromUnsortedList(head):
    if head == None:
        return None
    dummy = ListNode(0)
    dummy.next = head
    head = dummy
    s = set()
    while head.next != None:
        if head.next.val in s:
            head.next = head.next.next
        else:
            s.add(head.next.val)
            head = head.next
    return dummy.next


head = ListNode.arrayToList([1, 3, 2, 1, 4])
ListNode.printList(removeDuplicatesfromUnsortedList(head))
开发者ID:cutewindy,项目名称:CodingInterview,代码行数:32,代码来源:removeDuplicatesfromUnsortedList.py

示例4: middleofLinkedList

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import arrayToList [as 别名]
# Find the middle node of a linked list.

# Have you met this question in a real interview? Yes
# Example
# Given 1->2->3, return the node with value 2.

# Given 1->2, return the node with value 1.

from ListNode import ListNode

def middleofLinkedList(head):
    if head == None:
        return head

    slow = head
    fast = head.next
    while fast != None and fast.next != None:
        slow = slow.next
        fast = fast.next.next
    return slow


print middleofLinkedList(ListNode.arrayToList([1, 2, 3])).val
print middleofLinkedList(ListNode.arrayToList([1, 2])).val
开发者ID:cutewindy,项目名称:CodingInterview,代码行数:26,代码来源:middleofLinkedList.py

示例5: nthtoLastNodeinList

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import arrayToList [as 别名]
# Find the nth to last element of a singly linked list.

# The minimum number of nodes in list is n.

# Have you met this question in a real interview? Yes
# Example
# Given a List  3->2->1->5->null and n = 2, return node  whose value is 1.

from ListNode import ListNode

def nthtoLastNodeinList(head, n):
    if head is None or n is None:
        return None
    dummy = ListNode(0)
    dummy.next = head
    fast = head
    slow = head
    for i in range(n):
        fast = fast.next
    while fast is not None:
        fast =fast.next
        slow = slow.next
    return slow


head = ListNode.arrayToList([3, 2, 1, 5])
print nthtoLastNodeinList(head, 2).val
开发者ID:cutewindy,项目名称:CodingInterview,代码行数:29,代码来源:nthtoLastNodeinList.py

示例6: partitionList

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import arrayToList [as 别名]
# For example,
# Given 1->4->3->2->5->2->null and x = 3,
# return 1->2->2->4->3->5->null.

from ListNode import ListNode

def partitionList(head, x):

    # create two list:left and right
    leftDummy = ListNode(0)
    leftTail = leftDummy
    rightDummy = ListNode(0)
    rightTail = rightDummy

    while head != None:
        if head.val < x:
            leftTail.next = head
            leftTail = leftTail.next
        else:
            rightTail.next = head
            rightTail = rightTail.next
        head = head.next

    # combine two list
    rightTail.next = None
    leftTail.next = rightDummy.next

    return leftDummy.next

ListNode.printList(partitionList(ListNode.arrayToList([1, 4, 3, 2, 5, 2]), 3))
开发者ID:cutewindy,项目名称:CodingInterview,代码行数:32,代码来源:partitionList.py

示例7: deleteNodeintheMiddleofSinglyLinkedList

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import arrayToList [as 别名]
# Implement an algorithm to delete a node in the middle of a singly linked list,
#  given only access to that node.

# Have you met this question in a real interview? Yes
# Example
# Given 1->2->3->4, and node 3. return 1->2->4

from ListNode import ListNode

def deleteNodeintheMiddleofSinglyLinkedList(node):
    if node is None:
        return
    # node is not last node
    if node.next is not None:
        node.val = node.next.val
        node.next = node.next.next
    # node is the last node
    else:
        node = None


head = ListNode.arrayToList([1, 2, 3, 4])
node = head.next.next
deleteNodeintheMiddleofSinglyLinkedList(node)
ListNode.printList(head)
开发者ID:cutewindy,项目名称:CodingInterview,代码行数:27,代码来源:deleteNodeintheMiddleofSinglyLinkedList.py

示例8: len

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import arrayToList [as 别名]
    newCapacity = 2 * len(hashTable)
    newHashTable = [None for i in range(newCapacity)]
    for i in range(len(hashTable)):
        while hashTable[i] != None:
            newIndex = (hashTable[i].val % newCapacity + newCapacity) % newCapacity
            if newHashTable[newIndex] == None:
                newHashTable[newIndex] = ListNode(hashTable[i].val)
            else:
                dummy = newHashTable[newIndex]
                while dummy.next != None:
                    dummy = dummy.next
                dummy.next = ListNode(hashTable[i].val)
            hashTable[i] = hashTable[i].next

    return newHashTable

head1 = ListNode.arrayToList([21, 9])
head2 = ListNode.arrayToList([14])
hashTable = [None, head1, head2, None]
print rehashing(hashTable)










开发者ID:cutewindy,项目名称:CodingInterview,代码行数:22,代码来源:rehashing.py

示例9: reorderList

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import arrayToList [as 别名]
# Given a  singly linked list L: L0 -> L1 -> ... ->Ln-1 -> Ln,
# reorder it to: L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 -> ...

# You must do this in-place without altering the nodes' values.

# For example,
# Given 1 -> 2 -> 3 -> 4 -> null, reorder it to 1 -> 4 -> 2 -> 3 -> null.

from ListNode import ListNode

def reorderList(head):
    if head == None:
        return None


head = ListNode.arrayToList([1, 4, 2, 3])
ListNode.printList(reorderList(head))
开发者ID:cutewindy,项目名称:CodingInterview,代码行数:19,代码来源:reorderList.py

示例10: merge

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import arrayToList [as 别名]
        slow = slow.next
        fast = fast.next.next
    return slow


# helper: merge two sorted linked list
def merge(head1, head2):
    if head1 == None and head2 == None:
        return None

    dummy = ListNode(0)
    lastNode = dummy
    while head1 != None and head2 != None:
        if head1.val < head2.val:
            lastNode.next = head1
            head1 = head1.next
        else:
            lastNode.next = head2
            head2 = head2.next
        lastNode = lastNode.next
    if head1 != None:
        lastNode.next = head1
    else:
        lastNode.next = head2
    return dummy.next


head = ListNode.arrayToList([1, 3, 2, 4, 5, 7, 6])
ListNode.printList(sortList(head))
ListNode.printList(sortListI(head))
开发者ID:cutewindy,项目名称:CodingInterview,代码行数:32,代码来源:sortList.py

示例11: removeDupfromSortedListII

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import arrayToList [as 别名]
# Example
# Given 1->2->3->3->4->4->5, return 1->2->5.
# Given 1->1->1->2->3, return 2->3.

from ListNode import ListNode


def removeDupfromSortedListII(head):
    if head == None:
        return None

    dummy = ListNode(0)
    dummy.next = head
    head = dummy

    while head.next != None and head.next.next != None:
        if head.next.val == head.next.next.val:
            val = head.next.val
            while head.next != None and head.next.val == val:
                head.next = head.next.next
        else:
            head = head.next

    return dummy.next


head1 = ListNode.arrayToList([1, 2, 3, 3, 4, 4, 5])
head2 = ListNode.arrayToList([1, 1, 1, 2, 3])

ListNode.printList(removeDupfromSortedListII(head2))
开发者ID:cutewindy,项目名称:CodingInterview,代码行数:32,代码来源:removeDuplicatesfromSortedListII.py

示例12: removeLinkedListElements

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import arrayToList [as 别名]
# Remove all elements from a linked list of integers that have value val.

# Have you met this question in a real interview? Yes
# Example
# Given 1->2->3->3->4->5->3, val = 3, you should return the list as 1->2->4->5

from ListNode import ListNode

def removeLinkedListElements(head, val):
    if head == None or val == None:
        return head
    dummy = ListNode(0)
    dummy.next = head
    head = dummy
    while head.next != None:
        if head.next.val == val:
            head.next = head.next.next
        else:
            head = head.next
    return dummy.next

head = ListNode.arrayToList([1, 2, 3, 3, 4, 5, 3])
ListNode.printList(removeLinkedListElements(head, 3))

开发者ID:cutewindy,项目名称:CodingInterview,代码行数:25,代码来源:removeLinkedListElements.py

示例13: mergeKHelper

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import arrayToList [as 别名]
    mid = start + (end - start) / 2
    left = mergeKHelper(lists, start, mid)
    right = mergeKHelper(lists, mid + 1, end)
    return mergeTwoSortedList(left, right)

def mergeTwoSortedList(head1, head2):
    if head1 == None and head2 == None:
        return None
    dummy = ListNode(0)
    tail = dummy
    while head1 != None and head2 != None:
        if head1.val < head2.val:
            tail.next = head1
            head1 = head1.next
        else:
            tail.next = head2
            head2 = head2.next
        tail = tail.next
    if head1 != None:
        tail.next = head1
    else:
        tail.next = head2
    return dummy.next




head1 = ListNode.arrayToList([2, 4])
head2 = ListNode.arrayToList([-1])
ListNode.printList(mergeKSortedLists([head1, head2]))
开发者ID:cutewindy,项目名称:CodingInterview,代码行数:32,代码来源:mergeKSortedLists.py

示例14: removeDuplicatesfromSortedList

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import arrayToList [as 别名]
# Given a sorted linked list, delete all duplicates such that each element
# appear only once.

# Have you met this question in a real interview? Yes
# Example
# Given 1->1->2, return 1->2.
# Given 1->1->2->3->3, return 1->2->3.

from ListNode import ListNode

def removeDuplicatesfromSortedList(head):
    if head == None:
        return None
    node = head
    while node.next != None:
        if node.val == node.next.val:
            node.next = node.next.next
        else:
            node = node.next

    return head


# head1 = ListNode.arrayToList([1, 1, 2])
# ListNode.printList(removeDuplicatesfromSortedList(head1))
head2 = ListNode.arrayToList([1, 1, 2, 3, 3])
ListNode.printList(removeDuplicatesfromSortedList(head2))

开发者ID:cutewindy,项目名称:CodingInterview,代码行数:29,代码来源:removeDuplicatesfromSortedList.py


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