!!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. springcloud(六) - 配置中心

    功能介绍 设置和业务代码获取配置 功能实现 <!-- 添加configjar --> <dependency> <groupId>org.springframewo ...

  2. svn 中如何checkout出单个文件

    A 通过命令行操作 1.检出目录images svn co --depth=empty http://www.iusesvn.com/project1/images images_work_dir 这 ...

  3. vue vant3上传图片文件以流的形式上传

    axios.post("/fjt_fast/sys/comm/upload", { file: param.file}, { headers: { 'Content-Type': ...

  4. html的table多级表头表格的代码

    1,两级表头的代码 <html> <head> <title>多层表头</title> <link rel="stylesheet&qu ...

  5. MaaS模型即服务

    chatgpt的API开放让我看到了科研和产业结合的一种方式, 最新的科研成果也能飞入百姓家了. 也看到了MaaS的未来,估计要出现一批提供在线模型的新创公司了.

  6. Fedora 切换为阿里软件源

    1.备份原软件源配置 cp /etc/yum.repos.d/fedora.repo /etc/yum.repos.d/fedora.repo.bak cp /etc/yum.repos.d/fedo ...

  7. CANas分析软件,DBC文件解析,CAN报文分析,仿CANoe曲线显示

    2023.01.01:增加对Kvaser的支持参考了CANoe写了下面的软件,主要用途是对报文的回放及曲线的分析. 1.CAN连接,支持周立功CAN.CANFD及PCAN 2.DBC解析与生成文件 打 ...

  8. Vue+element ui 笔记

    1)可以直接拿过来就用的样式 https://www.cnblogs.com/xiao987334176/p/14188413.html 2)对Table里面的每一项全部设定为选中 mounted() ...

  9. slam面试题

    2022最新SLAM面试题汇总(持续更新中   https://blog.csdn.net/soaring_casia/article/details/125898830 candy边缘算子 http ...

  10. 一个MySQL双引号把我坑惨了!

    一.前言 最近经常碰到开发误删除误更新数据,这不,他们又给我找了个麻烦,我们来看下整个过程,把我坑得够惨. 二.过程 由于开发需要在生产环节中修复数据,需要执行120条SQL语句,需要将数据进行更新, ...