博客域名:
http://www.xnerv.wang
原题页面: https://oj.leetcode.com/problems/rotate-list/
题目类型:
难度评价:★
本文地址: http://blog.youkuaiyun.com/nerv3x3/article/details/38929137
原题页面: https://oj.leetcode.com/problems/rotate-list/
题目类型:
难度评价:★
本文地址: http://blog.youkuaiyun.com/nerv3x3/article/details/38929137
Given a list, rotate the list to the right by k places, where k is non-negative.
For example:
Given 1->2->3->4->5->NULL
and k = 2
,
return 4->5->1->2->3->NULL
.
class Solution:
# @param head, a ListNode
# @param k, an integer
# @return a ListNode
def rotateRight(self, head, k):
if None == head or None == head.next:
return head
cur_head = head
cur = head
while k > 0:
cur = cur.next
if None == cur:
return head
k -= 1
cur_tail = cur
cur_head = cur.next
if None == cur_head:
return head
cur = cur_head
while None != cur.next:
cur = cur.next
cur.next = head
cur_tail.next = None
return cur_head