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 ...
随机推荐
- Selenium入门系列4 选择并操作一组元素
选中一组元素的方式也是8种,与选中单个元素一一对应.区别只在于element与elements.elements取到的是一个数组,element取符合条件的第一个元素. 首先在脚本的目录下新建test ...
- 剑指offer39 平衡二叉树
剑指上用了指针传递,这里用的引用传递 class Solution { public: bool IsBalanced_Solution(TreeNode* pRoot) { ; return IsB ...
- node.js 练习3 调用函数
(1)创建n3-1.js,并输入代码 (2)创建User.js ,并输入代码 (3)运行cmd (4)在浏览器上查看 (5) 再次查看cmd
- 【Oracle-DBA】Oracle连接非常慢APPARENT DEADLOCK
我是一名软件包工头,哪里有问题就干哪里. 这次是 Oracle 出毛病了,我就临时兼了DBA的职,没办法,谁叫我是工头呢.打开百度就开干. 这次关键词是:APPARENT DEADLOCK!!! 丫的 ...
- 当Java遇见了Html--Jsp详解篇
###一.什么是Jsp jsp是一种基于文本的程序,全名java server page,其特点是html和java程序共存.执行时jsp会被运行容器编译,编译后的jsp跟servlet一样,因此js ...
- Linux赋予普通用户root权限
本文以新建用户admin来举例,请自行替换自己需要的用户 方法一: vi编辑 /etc/sudoers 文件,找到 root ALL=(ALL) ALL,在下面添加一行,如下所示: ## ...
- 详解LinkedHashMap如何保证元素迭代的顺序
大多数情况下,只要不涉及线程安全问题,Map基本都可以使用HashMap,不过HashMap有一个问题,就是迭代HashMap的顺序并不是HashMap放置的顺序,也就是无序.HashMap的这一缺点 ...
- ios中的三种弹框
目前为止,已经知道3种IOS弹框: 1.系统弹框-底部弹框 UIActionSheet (1)用法:处理用户非常危险的操作,比如注销系统等 (2)举例: UIActionSheet *sheet = ...
- Problem 1002-2017 ACM/ICPC Asia Regional Shenyang Online
网络赛:2017 ACM/ICPC Asia Regional Shenyang Online 题目来源:cable cable cable Problem Description: Connecti ...
- MySql错误1045 Access denied for user 'root'@'localhost' (using password:YES)
1.先停止mysql服务 2.进入mysql的安装路径,找到并打开my.ini文件,找到[mysqld],在该行下面添加 skip_grant_tables,也就是通知mysql,在登陆的时候跳过密码 ...