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 ...
随机推荐
- 华硕主板开启intel virtual technology以便支持虚拟机
- HTML入门2—HTML常用标签
HTML常用标签练习 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...
- 罗技G502设置
这个鼠标默认内置了3个档案模式,用G9键来调节. p2 蓝色 1个灯 p2 蓝色 2个灯 p3 蓝色 3个灯 如此循环设置
- 关于Ubuntu下安装Win8和Win8下安装Ubuntu的注意事项
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/svitter/article/details/32932387 本文出自:http://blog.c ...
- 【CCPC-Wannafly Winter Camp Day4 (Div1) H】命命命运(概率DP)
点此看题面 大致题意: 有\(6\)个人玩大富翁,共有\(n\)块地,进行\(500\)轮,已知每个人掷骰子掷出\(1\sim6\)的概率.当某人到达一块未被占领的地时,他可以占领它.求最后每个人占有 ...
- css术语和概念
.vocabulary{ height:99px; color:transparent; } 属性 上面示意css代码中的height和color就是属性. 值 上面的99px就是值 整数值: ...
- P1980 计数问题
题目描述 试计算在区间 11 到 nn的所有整数中,数字x(0 ≤ x ≤ 9)x(0≤x≤9)共出现了多少次?例如,在 11到1111中,即在 1,2,3,4,5,6,7,8,9,10,111,2, ...
- Compass Card Sales(模拟)
Compass Card Sales 时间限制: 3 Sec 内存限制: 128 MB提交: 35 解决: 13[提交] [状态] [讨论版] [命题人:admin] 题目描述 Katla has ...
- C# grid控件用数据库分页后台怎么写?
C#grid控件使用数据库分页的写法如下: mySystem.GetDataa(gridName.PageIndex *gridName.PageSize + 1, (gridName.PageInd ...
- gdb几个操作
如果进程转为守护进程,可设置如下跟进子进程 set follow-fork-mode child 输出变量/函数/返回值有print, call, display,自行选择 对于打印value has ...