!!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. gson TypeAdapter 和FieldNamingStrategy,SerializedName实现属性名称的设置别名

    gson TypeAdapter 和FieldNamingStrategy,SerializedName实现属性名称的设置别名 package com.example.core.mydemo.Type ...

  2. 常用IBatis属性

    <?xml version="1.0" encoding="utf-8" ?> <sqlMap namespace="GoodDet ...

  3. centos 更新git

    yum remove git rpm -ivh http://opensource.wandisco.com/centos/7/git/x86_64/wandisco-git-release-7-1. ...

  4. UltiSnips安装及设置

    2022-10-05 10:56:50 星期三 安装了UltiSnips插件,然后开始学习 第一个命令 UltiSnipsEdit 不好使,创建了~/.vim/UltiSnips 还是 can not ...

  5. Django Rest Frame work 如何使用serializers序列化函数新手教程

    Django Rest Frame work 如何使用serializers序列化       Django Rest Framework提供了serializers模块,用于序列化和反序列化模型实例 ...

  6. mybatis原理探究

    jdbc数据库运行流程: JDBC有哪三种statement接口: Statement 1.Statement接口提供了执行语句和获取结果的基本方法: 2.Statement继承自Wrapper:3. ...

  7. 实现分页数据请求的思路/Element UI(Plus)的分页模板(Vue3.x写法),(直接使用<script>引入vue.js)

    实现分页数据请求的思路/Element UI(Plus)的分页模板(Vue3.x写法),(直接使用<script>引入vue.js) 1. 效果图: 2.实现分页数据请求的思路:   分页 ...

  8. 【转载】win10怎么设置窗口护眼?

    转载地址:http://www.downza.cn/xy/122656.html win10设置窗口护眼模式的方法 2021-04-13 13:30:54来源:下载之家作者:down 小伙伴们你们知道 ...

  9. pgsql 查询结果和查询行数

    select count(*) over () as total, * from test

  10. [AGC043B] 123 Triangle

    个人思路: 首先,经过 \(1\) 轮就没有 \(3\) 了. 先考虑能否递推前 \(i\) 个数的答案,发现不行. 再考虑能否推出 \(i\) 个数的答案的计算公式,也发现不行. 然后就不会了. 正 ...