[Linked List]Rotate List
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.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
int getListLength(ListNode* head)
{
int len = ;
while(head){
len++;
head = head->next;
}
return len;
}
ListNode* rotateRight(ListNode* head, int k) {
int len = getListLength(head);
k = len == ? : len - k%len ;
if(head ==NULL || k== || k==len){
return head;
}
ListNode *newHead = NULL;
ListNode *cur = head;
int cnt = ;
while(cur){
++cnt;
if(cnt == k){
newHead = cur->next;
cur->next=NULL;
break;
}
cur = cur->next;
}
ListNode* p=newHead;
while(p && p->next){
p = p->next;
}
if(p){
p->next=head;
}
return newHead;
}
};
[Linked List]Rotate List的更多相关文章
- [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 ...
- 【LeetCode每天一题】Rotate List(旋转链表)
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
- [leetcode]61. Rotate List旋转链表
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
- LeetCode 61:旋转链表 Rotate List
给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. Given a linked list, rotate the list to the right by k pla ...
- [LeetCode] 61. Rotate List 旋转链表
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
- 力扣 — Rotate List()
题目描述: 中文: 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = ...
- [LC] 61. Rotate List
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
- 【LeetCode】61. Rotate List 解题报告(Python)
[LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- leetcode 旋转单链表
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
随机推荐
- android 5.0新特性CardView教程
CardView 是android5.0新加入的特性,大家先别着急,由于谷歌出了cardview的兼容包,也就是android.support.v7.widget.CardView包,所以在5.0以下 ...
- 防止输入时键盘覆盖掉textfiled
添加监听者 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardwasChange:) ...
- <转>Python的内存泄漏及gc模块的使用分析
一般来说在 Python 中,为了解决内存泄漏问题,采用了对象引用计数,并基于引用计数实现自动垃圾回收.由于Python 有了自动垃圾回收功能,就造成了不少初学者误认为自己从此过上了好日子,不必再受内 ...
- [STOI2014]舞伴(dp)
STOI是汕头OI...无聊翻到了去年的比赛题目,就写然后自己测了一下. 其实我很想吐槽为什么题目名是perm,perm好像和舞伴完全无关.. dp(x,s)=∑dp(x-1,s-{i}))(0< ...
- C++标准程序库读书笔记-第二章新的语言特性
1.基本类型的显式初始化 如果采用不含参数.明确的constructor(构造函数)调用语法,基本型别会被初始化为零: int i1; //undefined value int i2 = int() ...
- Python之路第十天,高级(2)-多线程,多进程,协程
线程 threading threading模块对象 描述 Thread 表示一个线程的执行对象 Lock 锁原语对象 RLock 可重入锁对象,使单线程可再次获得已经获得了的锁(递归锁定) Cond ...
- hdu 5823 color II 状压dp
题目链接 给n个点 n<=18. 然后给出它们两两之间是否有边相连. 问你这个图的所有子集,最少要用多少种颜色来染色, 如果两个点相连, 那么这两个点不能染同样的颜色. 先预处理出所有的点独立集 ...
- Vue.js实现拼图游戏
Vue.js实现拼图游戏 之前写过一篇<基于Vue.js的表格分页组件>的文章,主要介绍了Vue组件的编写方法,有兴趣的可以访问这里进行阅读:http://www.cnblogs.com/ ...
- 局域网内IP冲突怎么办
对于在Internet和Intranet网络上,使用TCP/IP协议时每台主机必须具有独立的IP地址,有了IP地址的主机才能与网络上的其它主机进行通讯.但IP地址冲突会造成网络客户不能正常工作,只 ...
- Oracle EBS-SQL (PO-15):检查不能审批的PO.sql
SELECT PHA.SEGMENT1, PHA.WF_ITEM_KEY, PHA.* FROM PO.PO_HEADERS_ALL PHA WHERE PHA.AUTHORIZATION_STA ...