Leetcode61
head
of a linked list, rotate the list to the right by k
places.!!class Solution(object):
def rotateRight(self, head, k):
if head is None or head.next is None: return head
start, end, len = head, None, 0
while head:
end = head
head = head.next
len += 1
end.next = start
pos = len - k % len
while pos > 1:
start = start.next
pos -= 1
ret = start.next
start.next = None
return ret
Leetcode61的更多相关文章
- [LeetCode61]Rotate List
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Giv ...
- [Swift]LeetCode61. 旋转链表 | Rotate List
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
- Leetcode61.旋转链表
链表中的点已经相连,一次旋转操作意味着: 先将链表闭合成环 找到相应的位置断开这个环,确定新的链表头和链表尾 class Solution{ public: ListNode* rotateRight ...
- Leetcode61. Rotate List旋转链表
给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = 2 输出: 4-& ...
- LeetCode链表解题模板
一.通用方法以及题目分类 0.遍历链表 方法代码如下,head可以为空: ListNode* p = head; while(p!=NULL) p = p->next; 可以在这个代码上进行修改 ...
随机推荐
- window JAVA 环境变量配置
java win环境变量配置1.安装的时候拷贝出,安装目录C:\Program Files\Java\jdk1.8.0_40\2.在系统变量中,点击"新建",弹出窗口后在变量名输入 ...
- eclipse设置UTF-8(字符编码集)所有方式
一.全部方式 1.修改workspace编码 windows->preferences->gengral->workspace 选择other->UTF-8. 2.修改Con ...
- Treetop Lights使用条款与免责协议
Treetop Lights (以下简称"我们")在此特别提醒您务必认真阅读.充分理解本<使用条款与免责协议>(以下简称"本协议")中各条款并选择是 ...
- C++ 用同一个raw pointer传入shared_ptr构造函数生成两个智能指针有什么问题?
Effective Modern C++ Item 19: use std::shared_ptr for shared-ownership resource Now, the constructor ...
- MySQL升级5.7.29
采用卸载后升级的方式 参考:https://blog.csdn.net/liu_dong_mei_mei/article/details/104010567 1.卸载原有的MySQL: 之前是wind ...
- “adb”不是内部或外部命令——解决方案
在AS(Android Studio简称AS)app真机测试中adb可以轻松找到安卓设备,ADB全称Android Debug Bridge,用于Android设备进行交互,也可以这样理解ADB是An ...
- 学习C语言哟
之前一直用的vs,感觉还不错,现在新发现了 一个Lightly工具,非常好用,各种环境自动配置好 看着新奇,比codeblocks好多了,各种玩意儿一大堆,不过也都行,只是这个安装轻松点 开始我的第二 ...
- docker-compose 文件
安装 curl -L https://get.daocloud.io/docker/compose/releases/download/v2.4.1/docker-compose-`uname -s` ...
- leetcode 剑指offer小结
栈与队列 剑指 Offer 09. 用两个栈实现队列 使用两个堆栈,一个输出栈,另一个输入栈.队列入队:直接将元素压入输入栈,队列出队:如果输出栈为空,将输入栈元素压入输出栈,再将输出栈元素出栈. 查 ...
- C# string的2个特殊方法
1. Strings.Join 方法:用指定的连接符连接一个数组中包含的若干子字符串创建的字符串.e.g List<string> list = new List<string> ...