!!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. [笔记] 什么是Groovy?什么是Kotlin?

    关于Groovy 参考文章 https://www.w3cschool.cn/groovy/ 摘抄如下: Groovy是JVM的一个替代语言(替代是指可以用 Groovy 在Java平台上进行 Jav ...

  2. 2022-11-19学习内容-Server端代码编写-Client端代码编写

    1.Server端代码编写 1.1UserDBHelper.java package com.example.chapter07_server.database; import android.con ...

  3. 三.database阶段回顾

    阶段回顾: 1.mysql:文件管理软件 2.三部分: 服务端 sql语句 客户端 3.客户端 mysql navicat 4.授权操作 用户操作 授权操作 5.sql语句 数据库操作 create ...

  4. 解决adb devices无法连接各种模拟器

    经常使用到模拟器的童鞋,如果在使用adb devices命令时,发现出现"List of devices attached",模拟器USB调试都开启的情况下,也没有连接成功.这种情 ...

  5. 戴尔n4110的独显驱动黄色感叹号问题的解决方法

    直接开门见山: 要下载旧版驱动R302028.exe,但是戴尔似乎下掉了,找到了代替的地址 下载地址如下: http://soft.onlinedown.net/soft/556280.htm ,下载 ...

  6. [Oracle19C ASM管理] 安装和配置ASM以及Oracle数据库

    一般设置 关闭防火墙 检查防火墙状态 systemctl status firewalld.service 暂时关闭防火墙,下次启动时防火墙仍随系统启动而启动 systemctl stop firew ...

  7. 生态学建模:增强回归树(BRT)预测短鳍鳗生存分布和影响因素|附代码数据

    全文下载链接: http://tecdat.cn/?p=22482 最近我们被客户要求撰写关于增强回归树(BRT)的研究报告,包括一些图形和统计输出. 在本文中,在R中拟合BRT(提升回归树)模型.我 ...

  8. ACCESS中查询语句:查询所得数据另存到一个指定目录下的文件夹里,并新建新的文件

    select * into [excel 8.0 database=d:\aa.xls].sheet1 from bb where bb.cc="dd" excel 8.0 为什么 ...

  9. 查看Sql数据库连接数

    select count(1) as nums, sd.name from  [Master].[dbo].[SYSPROCESSES] spinner join [Master].[dbo].[SY ...

  10. shell转义字符

    \n      回车 \b      单词边界 \w      [a-Z0-9]中任意一个字符 \W      [^a-Z0-9] \d      [0-9]中任意一个数字 \D      [^0-9 ...