原题地址:https://oj.leetcode.com/problems/rotate-list/

题意:

Given a list, rotate the list to the right by k places, where k is non-negative.

For example:
Given 1->2->3->4->5->NULL and k = 2,
return 4->5->1->2->3->NULL.

解题思路:循环右移一条链表,比如k=2,(1,2,3,4,5)循环右移两位变为(4,5,1,2,3)。由于k值有可能比链表长度大很多,所以先要用一个count变量求出链表的长度。而k%count就是循环右移的步数。

代码:

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @param head, a ListNode
# @param k, an integer
# @return a ListNode
def rotateRight(self, head, k):
if k == 0:
return head
if head == None:
return head
dummy = ListNode(0)
dummy.next = head
p = dummy
count = 0
while p.next:
p = p.next
count += 1
p.next = dummy.next
step = count - ( k % count )
for i in range(0, step):
p = p.next
head = p.next
p.next = None
return head

[leetcode]Rotate List @ Python的更多相关文章

  1. [leetcode]Rotate Image @ Python

    原题地址:https://oj.leetcode.com/problems/rotate-image/ 题意: You are given an n x n 2D matrix representin ...

  2. C++ STL@ list 应用 (leetcode: Rotate Array)

    STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...

  3. [LeetCode]题解(python):061-Rotate list

    题目来源 https://leetcode.com/problems/rotate-list/ Given a list, rotate the list to the right by k plac ...

  4. [LeetCode]题解(python):048-Rotate Image

    题目来源 https://leetcode.com/problems/rotate-image/ You are given an n x n 2D matrix representing an im ...

  5. [LeetCode] Rotate Array 旋转数组

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  6. [LeetCode] Rotate List 旋转链表

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  7. [LeetCode] Rotate Image 旋转图像

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  8. [LeetCode]题解(python):125 Valid Palindrome

    题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...

  9. [LeetCode]题解(python):120 Triangle

    题目来源 https://leetcode.com/problems/triangle/ Given a triangle, find the minimum path sum from top to ...

随机推荐

  1. LeetCode(53):最大子序和

    Easy! 题目描述: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: ...

  2. 【ES】代码例子

    #!/usr/bin/env python #coding=utf-8 from elasticsearch import Elasticsearch from elasticsearch_dsl i ...

  3. Unicode转义序列

    声明: web前端学习笔记,欢迎大神指点.联系QQ:1522025433. Javascipt 定义了一种特殊序列,使用6位ASCII字符代表任意16Unicode内码.这些Unicode转义序列均以 ...

  4. python 全栈开发,Day26(hashlib文件一致性,configparser,logging,collections模块,deque,OrderedDict)

    一.hashlib文件一致性校验 为何要进行文件一致性校验? 为了确保你得到的文件是正确的版本,而没有被注入病毒和木马程序.例如我们经常在网上下载软件,而这些软件已经被注入了一些广告和病毒等,如果不进 ...

  5. 2018-2019 2 20165203 《网络对抗技术》 Exp2 后门原理与实践

    2018-2019 2 20165203 <网络对抗技术> Exp2 后门原理与实践 实验内容 1.使用netcat获取主机操作Shell,cron启动 (0.5分) 2.使用socat获 ...

  6. 异构平台同步(mysql-->oracle)

    https://www.cnblogs.com/andy6/p/6159060.html

  7. asp.net core 微信公众号支付(扫码支付,H5支付,公众号支付,app支付)之3

    在微信公众号中访问手机网站,当需要调用支付时候无法使用H5支付,只有使用微信公众号支付,使用公众号支付用户必须关注该公众号同时该公众号必须开通公众号支付功能. 1.获取用户的OpenId ,参考之前写 ...

  8. Github+阿超运算

    感谢自己寒假能够稍稍做一点努力. Github个人页面<构建之法阅读笔记二可见>: https://github.com/Heartxy8990 申请教程: http://jingyan. ...

  9. GitHub安装教程

    第一步是安装两个软件 安装 git for windows这个是一个git的windows系统的命令行版本https://git-scm.com/downloads或者https://pan.baid ...

  10. 【Java】 剑指offer(34) 二叉树中和为某一值的路径

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 输入一棵二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有 ...