!!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. (python)正则表达式

    # -*- coding: utf-8 -*- import re def test_dot(): """ . => 表示匹配任意字符 ""&q ...

  2. 动态规划-1-钢条切割(Dynamic Programming-1-rod cutting)

    1 #include <stdio.h> 2 #define LEN 10 3 #define NEGINF -999999 4 struct r_d { 5 int r; //profi ...

  3. Windows11 微软提供三种路径 安装程序应用

    Windows11 微软提供三种路径 安装程序应用

  4. ES可视化平台kibana安装和使用

    一.kibana介绍 Kibana是一个针对Elasticsearch的开源分析及可视化平台,用来搜索.查看交互存储在Elasticsearch索引中的数据. 二.kibana安装 1.解压 tar ...

  5. debian / deepin (Ubuntu)安装 mariadb

    debian / deepin 安装 mariadb 安装步骤: LINUX安装mariadb本质和mysql一致,可以参考官网教程进行安装.https://mariadb.org/download/ ...

  6. Docker的资源限制

    CPU CGROUP MEM CGROUP Storage 默认的overlay fs不支持配额,需要底层文件系统如xfs,ext4的支持. 在docker中启用示例如下,限制单个容器最大使用空间为2 ...

  7. 前端项目线上部署记录 | vue-cli

    一.修改公开路径后打包;npm run build 新建一个vue.config.js文件,如果本地打开,则路径为"./',线上则'/',不加'.' module.exports = { p ...

  8. UGUI UI拖拽,UI连线。

    1.拖拽 public class Item : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { public Re ...

  9. 设置eclipse默认编码为UTF-8 Set default encoding to utf-8 in eclipse

    设置eclipse默认编码为UTF-8 Set default encoding to utf-8 in eclipse 1,windows->Perferences->General-& ...

  10. win10事件查看器出现10016错误的解决办法

    该错误一般会重复出现在事件查看器,严重的会导致系统卡死. 以解决下列错误为例,给出步骤: 注意记录用户(划掉的部分)及要添加的权限(本例为"本地激活"权限) 1.运行regedit ...