[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 ...
随机推荐
- URL 对特殊字符的处理
看到很多人说可以通过转移字符来进行转义,避免URL在请求的时候出错. 现在有了更好的方法了.不然还不得把半个ASCII码表给进行转义了! import java.io.UnsupportedEncod ...
- xmanager 在 Windows 下远程桌面连接 麒麟
编辑/etc/gdm/custom.conf,添加如下内容: [daemon] RemoteGreeter=/usr/libexec/gdmgreeter 注:“远程登录界面与本地登录界面相同”功能 ...
- Input输入字体颜色改变js(兼容IE)
从网上找的代码,自己封装了一下(前提:引用jQuery库) 方法1: HTML: <div class="box"> <div class="ipt1& ...
- MSDTC问题集
一.链接服务器的 OLE DB 访问接口 "SQLNCLI" 无法启动分布式事务. 尊重原著作:本文转载自http://sfwxw456.blog.163.com/blog/sta ...
- PHP学习笔记十【数组】
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/h ...
- 使用SQLCipher加密数据库
Xcode中集成了免费的sqlite,但是不提供加密的模块,突然有一天,蛋疼的客户要求把数据进行加密,于是乎就寻找使用简单并且可以把数据迁移过度到加密数据库的框架. SQLCipher是第三方的开 ...
- Zend Framework 框架搭建
通过手工方法搭建Zend Framework的MVC框架结构.首先看一下zend framework mvc的目录结构 1. 在根目录下面创建 public ,并在 public 下创建 index. ...
- hdu 4906 3-idiots fft
题目链接 n个火柴棍取3个, 问能组成三角形的概率是多少. kuangbin大神的博客写的很详细了..http://www.cnblogs.com/kuangbin/archive/2013/07/2 ...
- TypeError: 'QueryDict' object is not callable
id = int(request.POST('id')) Error message: TypeError: 'QueryDict' object is not callable Error rese ...
- Entity Framewor中的 Migration
http://www.entityframeworktutorial.net/code-first/code-based-migration-in-code-first.aspx = Code bas ...