leetcode 【Rotate List 】python 实现
题目:
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 实现的更多相关文章
- [leetcode]Rotate List @ Python
原题地址:https://oj.leetcode.com/problems/rotate-list/ 题意: Given a list, rotate the list to the right by ...
- [leetcode]Rotate Image @ Python
原题地址:https://oj.leetcode.com/problems/rotate-image/ 题意: You are given an n x n 2D matrix representin ...
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
- [LeetCode]题解(python):061-Rotate list
题目来源 https://leetcode.com/problems/rotate-list/ Given a list, rotate the list to the right by k plac ...
- [LeetCode]题解(python):048-Rotate Image
题目来源 https://leetcode.com/problems/rotate-image/ You are given an n x n 2D matrix representing an im ...
- [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 ...
- [LeetCode] Rotate List 旋转链表
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- [LeetCode] Rotate Image 旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- [LeetCode]题解(python):125 Valid Palindrome
题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...
- [LeetCode]题解(python):120 Triangle
题目来源 https://leetcode.com/problems/triangle/ Given a triangle, find the minimum path sum from top to ...
随机推荐
- CRUD全栈式编程概述
业务场景 CRUD,从数据驱动的角度几乎所有的的业务都是在做这样的事情. 几乎所有的操作都是在做对表的增删改查. 假设我们将数据库数据规个类: 分为基础/配置数据和业务/增长数据,或者说静态数据 ...
- IOS 制作版本新特性
创建版本新特性 页面(存放图片) HMNewfeatureViewController.m #define HMNewfeatureImageCount 4 #import "HMNewfe ...
- Ubuntu 16.04 安装札记
写在前面的话:第一次写,有点紧张,不知道怎么写,就胡乱写了,主要是写给自己看的,感觉这里大神遍地都是,大牛更是数不胜数,我就一小白,记下来怕以后忘了,言归正传,我初步打算是把这篇博客写成安装指南(捂脸 ...
- Centos 5.5 编译安装mysql 5.5.9
下载mysql wget http://mysql.mirrors.pair.com/Downloads/MySQL-5.5/mysql-5.5.9.tar.gz 创建mysql用户 [root@x ...
- python_3_coding
# -*- coding:utf-8 -*- 也可以换成下划线 用于声明文件编码,python3本身就是utf-8类型,不用声明 name="你好,世界"#utf-8格式能显示汉字 ...
- iis 发布失败原因总结
3篇文章 1. https://www.cnblogs.com/adzhouyang/p/7357086.html 2..https://blog.csdn.net/li_ser/article/de ...
- 【洛谷P1962】斐波那契数列
斐波那契数列 题目链接:https://www.luogu.org/problemnew/show/P1962 矩阵A 1,1 1,0 用A^k即可求出feb(k). 矩阵快速幂 #include&l ...
- 第23章 I2C—读写EEPROM—零死角玩转STM32-F429系列
第23章 I2C—读写EEPROM 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/f ...
- 头部导航悬浮,css
.header{ position:fixed; z-index:100; left:; right:; } 如图.
- 一篇RxJava友好的文章(二)
上一篇文章介绍了rxjava的基本用法,和一些常用的操作符,以及rxjava的链式操作带来的好处.由于rxjava非常的强大,让我如此的痴迷,我打算写五篇文章,专门讲解rxjava 常见的操作符和用法 ...