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


Python ListNode.printList方法代码示例

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


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

示例1: removeDuplicatesfromUnsortedList

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import printList [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

示例2: ListNode

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import printList [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

示例3: deleteNodeintheMiddleofSinglyLinkedList

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import printList [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

示例4: mergeTwoSortedLists

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import printList [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

示例5: reorderList

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import printList [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

示例6: partitionList

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import printList [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: removeDupfromSortedListII

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import printList [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

示例8: merge

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import printList [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

示例9: removeLinkedListElements

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import printList [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

示例10: swapNodesinPairs

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

# Challenge
# Your algorithm should use only constant space. You may not modify the values
# in the list, only nodes itself can be changed.

from ListNode import ListNode

def swapNodesinPairs(head):
    if head == None:
        return None
    dummy = ListNode(0)
    dummy.next = head
    head = dummy
    while head != None and head.next != None:
        n1 = head.next
        n2 = head.next.next
        # head -> n1 -> n2 ... => head -> n2 -> n1 ...
        head.next = n2
        n1.next = n2.next
        n2.next = n1

        # move to next pair
        head = n1
    return dummy.next


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

示例11: mergeKHelper

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import printList [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

示例12: O

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import printList [as 别名]
# After removing the second node from the end, the linked list becomes
# 1->2->3->5->null.
# Note
# The minimum number of nodes in list is n.

# Challenge
# O(n) time

from ListNode import ListNode

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

    slow.next = slow.next.next
    return dummy.next


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

示例13: range

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import printList [as 别名]
    head = dummy

    # find premmNode and mNode
    for i in range(1, m):
        head = head.next
    prevmNode = head
    mNode = head.next

    # reverse link from m to n
    nNode = mNode
    nextnNode = nNode.next
    for i in range(m, n):
        temp = nextnNode.next
        nextnNode.next = nNode
        nNode = nextnNode
        nextnNode = temp

    # combine
    prevmNode.next = nNode
    mNode.next = nextnNode

    return dummy.next


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




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

示例14: removeDuplicatesfromSortedList

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import printList [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

示例15: reverseLinkedList

# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import printList [as 别名]
# Reverse a linked list.

# Have you met this question in a real interview? Yes
# Example
# For linked list 1->2->3, the reversed linked list is 3->2->1

# Challenge
# Reverse it in-place and in one-pass

from ListNode import ListNode


def reverseLinkedList(head):
    prev = None
    while head != None:
        temp = head.next
        head.next = prev
        prev = head
        head = temp
    return prev


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


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