!!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. oracle排查慢sql

    查询最慢的SQL select * from (select sa.SQL_TEXT, sa.SQL_FULLTEXT, sa.EXECUTIONS "执行次数", round(s ...

  2. win10关闭自动更新的方法

    win10关闭自动更新的方法和步骤: 一.禁用Windows Update服务 1.打开服务项,win+r 输入 services.msc ,或者控制面板-管理工具-服务. 2.找到 Windows ...

  3. Prettier 与 ESLint 对函数名后空格的处理

    问题 Prettier 格式化 JavaScript 代码之后,默认不会在函数与 () 添加空格,而 ESLint 默认情况下则要求函数与 () 之间必须有一个空格. 保留空格的方案配置 方案 1 在 ...

  4. 抓包工具Fidder

    第一步:直接在浏览器下载 第二步:设置Fiddler打开Fiddler,     Tools-> Fiddler Options (配置完后记得要重启Fiddler)选中"Decrpt ...

  5. codeforces 165C Another Problem on Strings 二分or双指针

    题意:给一个01字符串s,找出s包含恰好k个1的连续字串个数 解法: 显然是简单的双指针or二分的题,但由于k=0的存在,使得双指针的边界条件十分难写,所以应该选择二分! #include<bi ...

  6. 扩linux 主分区 xfs 格式的

    在centos8 上 扩主分区 # 扩系统盘,第一个分区 growpart /dev/vda 1 #后面加挂接点 一般是 / xfs_growfs  / # 查看 df -h

  7. https原理(四)双向实践(java客户端+tcp代理)

    本文采用客户端与服务端共用一个密钥对 1 将https代理服务器(三)实践中的mkcert p12分解为一个公钥一个私钥 mac@macdeMacBook mkcert % openssl pkcs1 ...

  8. .NetCore自定义模板,发布Nuget

    1.创建模板项目框架 2.创建模板文件 在项目文件夹根目录创建.template.config文件夹,在文件夹下创建新的文件:template.json 内容如下 { "$schema&qu ...

  9. 集成GIT仓库

    集成GIT仓库 jgit - java实现git操作 一个 Java 程序中使用 Git ,有一个功能齐全的 Git 库,那就是 JGit . JGit 是一个用 Java 写成的功能相对健全的 Gi ...

  10. windows 服务 包装模板

    github地址:  https://github.com/xl711436/Xiaolei.MockService 在 MockServiceInstance.cs 中 对应的方法中添加 对应的逻辑 ...