您的位置:首页 > 其它

LeetCode-61-Rotate List 链表水题

2017-09-20 16:32 357 查看
# 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 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: