LeetCode-61-Rotate List 链表水题

来源:互联网 发布:格兰杰詹姆斯数据 编辑:程序博客网 时间:2024/05/01 10:28


# Definition for singly-linked list.# class ListNode(object):#     def __init__(self, x):#         self.val = x#         self.next = Noneclass Solution(object):    def rotateRight(self, head, k):        """        :type head: ListNode        :type k: int        :rtype: ListNode        """        if head==None:return []        cnt=1        last=head        while(last.next!=None):            last=last.next            cnt+=1        k%=cnt        if k==0:return head        curHead=head        last.next=head        print last.val        while(cnt>k+1):            cnt-=1            curHead=curHead.next        ans=curHead.next                curHead.next=None        return ans        


原创粉丝点击