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


Python LinkedList.addNode方法代码示例

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


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

示例1: partition

# 需要导入模块: import LinkedList [as 别名]
# 或者: from LinkedList import addNode [as 别名]
def partition(linkedList, x):
    small, large = LinkedList(), LinkedList();
    node = linkedList.head
    while node:
        if node.value < x:
            small.addNode(node.value)
        else:
            large.addNode(node.value)
        node = node.next
    small.tail.next = large.head
    return small
开发者ID:Haruuuko,项目名称:CTCI_5th,代码行数:13,代码来源:question2_4.py

示例2: addList

# 需要导入模块: import LinkedList [as 别名]
# 或者: from LinkedList import addNode [as 别名]
def addList(list1, list2):
    a, b = list1.head, list2.head
    result = LinkedList()
    temp = 0
    while a and b:
        num = (a.value + b.value + temp) % 10
        result.addNode(num)
        temp = (a.value + b.value + temp) / 10
        a, b = a.next, b.next
    if a:
        result.addNode(a.value + temp)
        reuslt.tail.next = a.next
    elif b:
        result.addNode(b.value + temp)
        result.tail.next = b.next
    return result
开发者ID:Haruuuko,项目名称:CTCI_5th,代码行数:18,代码来源:question2_5.py

示例3: linkedlist

# 需要导入模块: import LinkedList [as 别名]
# 或者: from LinkedList import addNode [as 别名]
            else:
                smallEnd.next = node
                smallEnd = smallEnd.next
        else:
            if not largeStart:
                largeStart = largeEnd = node
            else:
                largeEnd.next = node
                largeEnd = largeEnd.next
        node = node.next

    if smallEnd is None:
        return linkedlist(largeStart)

    smallEnd.next = largeStart

    return LinkedList(smallStart)


if __name__ == "__main__":
    nodes = [Node(i) for i in range(10)]
    nodes.extend([Node(3), Node(5)])
    nodes.insert(1, Node(8))

    linkedlist = LinkedList()
    for node in nodes:
        linkedlist.addNode(node)
    print linkedlist

    print partitionList(linkedlist, 5)
开发者ID:yangspeaking,项目名称:cc150,代码行数:32,代码来源:2.4.partition_linkedlist.py

示例4: isPalindrome

# 需要导入模块: import LinkedList [as 别名]
# 或者: from LinkedList import addNode [as 别名]
from LinkedList import *

def isPalindrome(linkedList):
    newlist = []
    slow, fast = linkedList.head, linkedList.head
    while fast and fast.next:
        newlist.append(slow.value)
        slow, fast = slow.next, fast.next.next
    if fast: slow = slow.next
    while slow:
        if slow.value != newlist.pop():
            return False
        slow = slow.next
    return True

test = LinkedList()
test.addNode(1)
test.addNode(2)
test.addNode(3)
test.addNode(4)
test.addNode(3)
test.addNode(2)
test.addNode(1)

print(test)
print(isPalindrome(test))

    
开发者ID:Haruuuko,项目名称:CTCI_5th,代码行数:28,代码来源:question2_7.py


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