题目

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.

代码:oj在线测试通过 Runtime: 200 ms

 # 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 or head is None or head.next is None:
return head dummyhead = ListNode(0)
dummyhead.next = head pFirst = dummyhead
pSecond = dummyhead # get length of the linked list
length = 0
p = dummyhead
while p.next is not None:
length += 1
p = p.next
k = k % length
if k == 0:
return dummyhead.next for i in range(0,k):
pFirst = pFirst.next while pFirst.next is not None:
pFirst = pFirst.next
pSecond = pSecond.next result = pSecond.next
pSecond.next = None
pFirst.next = dummyhead.next return result

思路

这个题目感觉没有说清楚 如果k大于表长度应该怎么办 并不是特别严谨

首先对k值进行预处理(尤其需要考虑k大于表长度的情况)

1. 处理一个special case: 当k等于表长的时候 不用处理 直接返回Linked List (这个case之前一直没有考虑,导致一直没有通过,shit)

2. 后面的就是常规的思路。双指针,其中一个指针先移动k步;然后两个指针一起移动,第一个指针移动到最后一个元素;再然后就是把尾巴接到头上,再从第二个指针.next的位置向后断开就OK了

疑惑:小白还有一个疑惑 就是如何才能不把k=0的情况当杜作为一个case考虑?请鹿过高手拍砖并指点.

leetcode 【Rotate List 】python 实现的更多相关文章

  1. [leetcode]Rotate List @ Python

    原题地址:https://oj.leetcode.com/problems/rotate-list/ 题意: Given a list, rotate the list to the right by ...

  2. [leetcode]Rotate Image @ Python

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

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

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

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

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

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

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

  6. [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  ...

  7. [LeetCode] Rotate List 旋转链表

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

  8. [LeetCode] Rotate Image 旋转图像

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

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

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

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

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

随机推荐

  1. IOS 发布程序(打包上传)

    • 发布程序的主要步骤 登录开发者主页 生成cer证书:cer是一个跟电脑相关联的证书文件,让电脑具备发布程序的功能 添加App ID:发布哪些app? 生成MobileProvision文件:生成一 ...

  2. 2018.7.18 div,section,article的区别和使用

    section ·<section> 标签定义文档中的节(section.区段).比如章节.页眉.页脚或文档中的其他部分. ·section用作一段有专题性的内容,一般在它里面会带有标题. ...

  3. EF core 学习笔记

    应该 以领域 为核心开发程序, 不应该 以数据库 entityframeworkcore entityframeworkcore.sqlserver entityframeworkcore.tool ...

  4. 第10章 新建工程-库函数版—零死角玩转STM32-F429系列

    第10章     新建工程—库函数版 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fire ...

  5. youku服务端

    文件结构 config import os IP_PORT = () BACKLOG = BASE_DIR = os.path.dirname(os.path.dirname(__file__)) B ...

  6. React Native ref高级用法&&setNativeProps使用

    ref属性不只是string ref属性不仅接受string类型的参数,而且它还接受一个function作为 callback.这一特性让开发者对ref的使用更加灵活. render() { retu ...

  7. XCode快捷键使用

    :first-child{margin-top:0!important}.markdown-body>:last-child{margin-bottom:0!important}.markdow ...

  8. LeetCode961 重复 N 次的元素

    问题: 重复 N 次的元素 在大小为 2N 的数组 A 中有 N+1 个不同的元素,其中有一个元素重复了 N 次. 返回重复了 N 次的那个元素. 示例 1: 输入:[1,2,3,3] 输出:3 示例 ...

  9. linux文件属性更改命令

    chown 当我们要改变一个文件的属主,我们所使用的用户必须是该文件的属主而且同时是目标属组成员,或超级用户.只有超级用户的才能改变文件的属主. chown语法: chown  [选项]...[所有者 ...

  10. IntelliJ IDEA 12详细开发教程(一)思想的转变与新手入门【转】

    转载地址:http://bangqu.com/alicas/blog/433 从事软件开发工作以来,提高自己的开发效率,提高自己编码的规范,提高编码深度层次,这三样一直都是自己努力去追求的事情. 最近 ...