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 ...
随机推荐
- ARM实验2 —— 蜂鸣器实验
PWM蜂鸣器实验: 实验内容: 编写PWM模块程序,通过PWM控制FS_4412平台上的蜂鸣器. 实验目的: 熟悉开发环境. 掌握exynos4412处理器的PWM功能. 实验平台: FS_4412开 ...
- 罗技G502设置
这个鼠标默认内置了3个档案模式,用G9键来调节. p2 蓝色 1个灯 p2 蓝色 2个灯 p3 蓝色 3个灯 如此循环设置
- xrdp 安装后 WINDOWS远程登录出错
xrdp需要vnc作为基础服务, sudo apt-get install tightvncserver 树莓派上这个命令运行下再连就好了
- 关于requireJS的同步加载和异步加载
这篇随笔主要记录require('name')和require(['name1','name2'])在同步和异步加载使用的区别 1.require('name')同步加载模块的形式 define(fu ...
- tarjan+topsort
题目 缩完点后统计入读为零的点就可以来. 因为缩完点后肯定是DAG #include<iostream> #include<cstdio> #include<algori ...
- JS实现数组去重的方法(6种)
方法一: 双层循环,外层循环元素,内层循环时比较值 如果有相同的值则跳过,不相同则push进数组 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Arra ...
- lintcode_111_爬楼梯
爬楼梯 描述 笔记 数据 评测 假设你正在爬楼梯,需要n步你才能到达顶部.但每次你只能爬一步或者两步,你能有多少种不同的方法爬到楼顶部? 您在真实的面试中是否遇到过这个题? Yes 哪家公司问你的 ...
- python--Matplotlib(一)
基础知识薄弱的同学可以看看一下博客 https://www.cnblogs.com/dev-liu/p/pandas_plt_basic.html https://blog.csdn.net/Notz ...
- javaScript的闭包 js变量作用域
js的闭包 js的变量作用域: var a=90; //定义一个全局变量 function test(){ a=123; //使用外层的 a变量 } test(); document.write(&q ...
- float 浮动详解
浮动(float):浮动原先设定时主要是用于文本环绕图像设定的,后来发现其在css布局中有很大的帮助,故渐渐使用浮动. 浮动后的元素脱离了文档的普通流,使得浮动的元素不占据文档的位置,其他元素可以覆盖 ...