• 链表_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)

  • 相关阅读:
    At least one JAR was scanned for TLDs yet contained no TLDs
    {转} MJPG流媒体在HTML5的呈现方案
    {转}理解HTTP/304响应
    blueimp,预览遮罩范围控制
    快速生成mysql上百万条测试数据
    mysql插入文本文档及读取
    tomcat启动报错:注释指定的bean类.与现有的冲突.相同的名称和类
    csv的文件excel打开长数字后面位变0的解决方法
    UNIX或LINUX时间戳转时间
    Myeclipse更新SVNStatusSubscriber 时报告了错误。1 中的 0 个资源已经同步。
  • 原文地址:https://www.cnblogs.com/lux-ace/p/10557148.html
Copyright © 2020-2023  润新知