您的位置:首页 > 编程语言 > Python开发

【LeetCode with Python】 Rotate List

2014-08-29 18:34 211 查看
博客域名:http://www.xnerv.wang

原题页面:https://oj.leetcode.com/problems/rotate-list/

题目类型:

难度评价:★

本文地址:/article/1377489.html

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