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


Python LinkedList.getRandomLinkedList方法代码示例

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


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

示例1: rotateRight

# 需要导入模块: import LinkedList [as 别名]
# 或者: from LinkedList import getRandomLinkedList [as 别名]
    # @return the head node in the linked list
   def rotateRight(self, A, B):
        length = self.getLength(A)
        if length == 1:
            return A
        B = B % length
        if B == 0:
            return A
        temp = A
        for _ in xrange(length-B-1):
            temp = temp.next
        head = temp.next
        temp.next = None
        temp = head
        while temp.next is not None:
            temp = temp.next
        temp.next = A
        return head
    
    def getLength(self, A):
        length = 0
        while A is not None:
            length += 1
            A = A.next
        return length

sol = Solution()
A = LinkedList.getRandomLinkedList(2)
LinkedList.printLinkedList(A)
A = sol.rotateRight(A, 2)
LinkedList.printLinkedList(A)
开发者ID:karandevgan,项目名称:Algorithms,代码行数:33,代码来源:RotateLinkedList.py

示例2: swapPairs

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

class Solution:
    # @param A : head node of linked list
    # @return the head node in the linked list
    def swapPairs(self, A):
        start = LinkedList.ListNode("dummy")
        start.next = A
        current = start

        while current.next and current.next.next:
            current.next = self.swapNodes(current.next, current.next.next)
            current = current.next.next
        return start.next
    
    def swapNodes(self, A, B):
        A.next = B.next
        B.next = A
        return B

import LinkedList
A = LinkedList.getRandomLinkedList(11, 20)
LinkedList.printLinkedList(A)
sol= Solution()
B = sol.swapPairs(A)
LinkedList.printLinkedList(B)
开发者ID:karandevgan,项目名称:Algorithms,代码行数:28,代码来源:SwapNodes.py

示例3: ListNode

# 需要导入模块: import LinkedList [as 别名]
# 或者: from LinkedList import getRandomLinkedList [as 别名]
        while B is not None:
            sum = B.val + carry
            carry = sum / 10
            C.next = ListNode(sum % 10)
            C = C.next
            B = B.next
        
        if carry != 0:
            C.next = ListNode(carry)

        return returnHead
    
    def reverseLinkedList(self, A):
        prev = None
        while A is not None:
            temp = A.next
            A.next = prev
            prev = A
            A = temp
        return prev


A = LinkedList.getRandomLinkedList(3)
B = LinkedList.getRandomLinkedList(5)
LinkedList.printLinkedList(A)
LinkedList.printLinkedList(B)

sol = Solution()
C = sol.addTwoNumbers(A,B)
LinkedList.printLinkedList(C)
开发者ID:karandevgan,项目名称:Algorithms,代码行数:32,代码来源:AddNumbersAsList.py


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