[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 ...
随机推荐
- Spring--------web应用中保存spring容器
---恢复内容开始--- 问题:在一个web应用中我使用了spring框架,但有一部分模块或组件并没有托管给Spring,比如有的可能是一个webservice服务类,如果我想在这些非托管的类里使用托 ...
- myeclipse内存不足有关问题
myeclipse内存不足有关问题 myeclipse内存不足问题 使用myeclipse8.5出现如下问题:MyEclipse has detected that less than 5% of t ...
- stringstream函数(i o)
stringstream函数 头文件 #include<sstream> stringstream是字符串流,被用来切分数据或转化类型 样例一(摘) 输入n,代表接下来输入n行资料,每行 ...
- ThinkPHP 类似Yii的Gii生成Model的功能。
ThinkPHP 类似Yii的Gii生成Model的功能.自动生成ThinkPhp 3.1 的基础模型.. #!/usr/bin/env php <?php /** * * THINKPHP 基 ...
- JSON入门之二:org.json的基本用法
java中用于解释json的主流工具有org.json.json-lib与gson,本文介绍org.json的应用. 官方文档: http://www.json.org/java/ http://de ...
- tab标签切换(无炫效果,简单的显示隐藏)
从最简单的效果开始写起,一个简单的JQ写出tab切换效果,很静态,没有任何的轮转特效,单纯的点击标签显示区域块. 附上代码: HTML: <div class="wrapper&quo ...
- python核心编程-第三章-个人笔记
1.语句和语法 (1)反斜杠"\"表示语句继续.python良好的编程习惯是一行最后不超过80个字符,一行字符过多时便须用到反斜杠换行继续该语句. PS:在使用小括号.中括号.大括 ...
- Oracle EBS-SQL (OM-6):打开订单.sql
/*打开头*/ update oe_order_headers_all t set t.flow_status_code = 'BOOKED', t.open_flag = 'Y' where t.o ...
- linux之SQL语句简明教程---CONCATENATE
有的时候,我们有需要将由不同栏位获得的资料串连在一起.每一种资料库都有提供方法来达到这个目的: MySQL: CONCAT( ) Oracle: CONCAT( ), || SQL Server: + ...
- spark aggregate
该函数官方的api,说的不是很明白: aggregate(zeroValue, seqOp, combOp) Aggregate the elements of each partition, and ...