zoukankan      html  css  js  c++  java
  • 链表_leetcode61

    # Definition for singly-linked list.
    class ListNode(object):
    def __init__(self, x):
    self.val = x
    self.next = None

    class Solution(object):
    def rotateRight(self, head, k):
    """
    :type head: ListNode
    :type k: int
    :rtype: ListNode
    """


    if not head or not head.next:
    return head

    dummyHead = ListNode(0)
    dummyHead.next = head

    pre = dummyHead

    tail = head
    sum = 1

    while tail.next:
    pre = pre.next
    tail = tail.next
    sum += 1



    count = k % sum

    while count:

    pre.next = tail.next
    tail.next = dummyHead.next
    dummyHead.next = tail

    pre , tail = self.getTail(dummyHead.next)


    return dummyHead.next


    def getTail(self,head):
    pre = head
    tail = head.next

    while tail.next:
    pre = tail
    tail = tail.next

    return pre ,tail


    def creatList(self,l):
    dummyHead = ListNode(0)

    pre = dummyHead

    for i in l:
    pre.next = ListNode(i)
    pre = pre.next

    return dummyHead.next


    def printList(self,head):
    cur = head

    while cur:
    print cur.val
    cur = cur.next



    l1 = [1,2,3,4,5,6]

    k = 2

    s = Solution()# Definition for singly-linked list.
    class ListNode(object):
    def __init__(self, x):
    self.val = x
    self.next = None

    class Solution(object):
    def rotateRight(self, head, k):
    """
    :type head: ListNode
    :type k: int
    :rtype: ListNode
    """


    if not head or not head.next:
    return head

    dummyHead = ListNode(0)
    dummyHead.next = head

    pre = dummyHead

    tail = head
    sum = 1

    while tail.next:
    pre = pre.next
    tail = tail.next
    sum += 1



    count = k % sum

    # pre.next = tail.next
    # tail.next = dummyHead.next
    # dummyHead.next = tail
    #
    # self.printList(dummyHead.next)
    #
    # pre, tail = self.getTail(dummyHead.next)
    #
    # # print pre.val
    # # print tail.val
    #
    # pre.next = tail.next
    # tail.next = dummyHead.next
    # dummyHead.next = tail
    #
    # self.printList(dummyHead.next)


    while count > 0:

    pre.next = tail.next
    tail.next = dummyHead.next
    dummyHead.next = tail

    pre , tail = self.getTail(dummyHead.next)

    count -= 1


    return dummyHead.next


    def getTail(self,head):
    pre = head
    tail = head.next

    while tail.next:
    pre = tail
    tail = tail.next

    return pre ,tail


    def creatList(self,l):
    dummyHead = ListNode(0)

    pre = dummyHead

    for i in l:
    pre.next = ListNode(i)
    pre = pre.next

    return dummyHead.next


    def printList(self,head):
    cur = head

    while cur:
    print cur.val
    cur = cur.next



    l1 = [1,2,3,4,5]

    k = 2

    s = Solution()

    head = s.creatList(l1)

    s.printList(head)

    s.rotateRight(head,2)

  • 相关阅读:
    vue第一天
    wnf微信公众号总结
    node-sass安装失败解决方法
    移动app第一天
    sublime的快捷键(常用)
    JMeter——项目——导出结果到excel(先导入数据库——再从数据库导出为excel)
    JMeter——项目——导出结果到excel
    JMeter——项目——注册、登录、充值、保存执行结果
    JMeter——cookie管理
    JMeter——配置元件——httpcookie管理器
  • 原文地址:https://www.cnblogs.com/lux-ace/p/10557148.html
Copyright © 2011-2022 走看看