!!Given the head of a linked list, rotate the list to the right by k places.!!
 
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def rotateRight(self, head, k):
        """
        :type head: ListNode
        :type k: int
        :rtype: ListNode
        """
        if head == None:
            return []
        count = 1
        temp = head
        while (temp.next != None):
            count +=1
            temp = temp.next
            # count = length, so the length remaining same seq is count - k -1
 
        if k == 0:return head;
        #else
        head = temp.next
        temp.next = None
        while(count -k -1): #ensure the amount of reversed nodes
            #to reverse
            head = temp.next
            temp.next = None #clearance
            return head
 
A better way:
#先把链表首尾相连,再找到位置断开循环
class Solution(object):
def rotateRight(self, head, k):
if head is None or head.next is None: return head
start, end, len = head, None, 0
while head:
end = head
head = head.next
len += 1
end.next = start
pos = len - k % len
while pos > 1:
start = start.next
pos -= 1
ret = start.next
start.next = None
return ret

Leetcode61的更多相关文章

  1. [LeetCode61]Rotate List

    题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Giv ...

  2. [Swift]LeetCode61. 旋转链表 | Rotate List

    Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...

  3. Leetcode61.旋转链表

    链表中的点已经相连,一次旋转操作意味着: 先将链表闭合成环 找到相应的位置断开这个环,确定新的链表头和链表尾 class Solution{ public: ListNode* rotateRight ...

  4. Leetcode61. Rotate List旋转链表

    给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = 2 输出: 4-& ...

  5. LeetCode链表解题模板

    一.通用方法以及题目分类 0.遍历链表 方法代码如下,head可以为空: ListNode* p = head; while(p!=NULL) p = p->next; 可以在这个代码上进行修改 ...

随机推荐

  1. window JAVA 环境变量配置

    java win环境变量配置1.安装的时候拷贝出,安装目录C:\Program Files\Java\jdk1.8.0_40\2.在系统变量中,点击"新建",弹出窗口后在变量名输入 ...

  2. eclipse设置UTF-8(字符编码集)所有方式

    一.全部方式 1.修改workspace编码 windows->preferences->gengral->workspace 选择other->UTF-8.  2.修改Con ...

  3. Treetop Lights使用条款与免责协议

    Treetop Lights (以下简称"我们")在此特别提醒您务必认真阅读.充分理解本<使用条款与免责协议>(以下简称"本协议")中各条款并选择是 ...

  4. C++ 用同一个raw pointer传入shared_ptr构造函数生成两个智能指针有什么问题?

    Effective Modern C++ Item 19: use std::shared_ptr for shared-ownership resource Now, the constructor ...

  5. MySQL升级5.7.29

    采用卸载后升级的方式 参考:https://blog.csdn.net/liu_dong_mei_mei/article/details/104010567 1.卸载原有的MySQL: 之前是wind ...

  6. “adb”不是内部或外部命令——解决方案

    在AS(Android Studio简称AS)app真机测试中adb可以轻松找到安卓设备,ADB全称Android Debug Bridge,用于Android设备进行交互,也可以这样理解ADB是An ...

  7. 学习C语言哟

    之前一直用的vs,感觉还不错,现在新发现了 一个Lightly工具,非常好用,各种环境自动配置好 看着新奇,比codeblocks好多了,各种玩意儿一大堆,不过也都行,只是这个安装轻松点 开始我的第二 ...

  8. docker-compose 文件

    安装 curl -L https://get.daocloud.io/docker/compose/releases/download/v2.4.1/docker-compose-`uname -s` ...

  9. leetcode 剑指offer小结

    栈与队列 剑指 Offer 09. 用两个栈实现队列 使用两个堆栈,一个输出栈,另一个输入栈.队列入队:直接将元素压入输入栈,队列出队:如果输出栈为空,将输入栈元素压入输出栈,再将输出栈元素出栈. 查 ...

  10. C# string的2个特殊方法

    1. Strings.Join 方法:用指定的连接符连接一个数组中包含的若干子字符串创建的字符串.e.g List<string> list = new List<string> ...