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)

  • 相关阅读:
    PBE加密 .net 实现
    手把手教你写一个windows服务 【基于.net】 附实用小工具{注册服务/开启服务/停止服务/删除服务}
    fish redux 个人理解
    .net Core 图片验证码 基于SkiaSharp实现
    .net core webapi jwt 更为清爽的认证 ,续期很简单(2)
    js删除数组对象中符合条件的数据
    编译.net .net Core程序 代码,仅做备份
    Mybatis架构相关的知识
    java选择题知识总结大全
    Mybatis详解(二) sqlsession的创建过程
  • 原文地址:https://www.cnblogs.com/lux-ace/p/10557148.html
Copyright © 2011-2022 走看看