【leetcode】Rotate List(middle)
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.
题目关键点:k可能比链表长度还大, 需要获取链表长度len, 用 k % len 获取具体需要移动的数字。
class Solution {
public:
ListNode *rotateRight(ListNode *head, int k) {
if(head == NULL || head->next == NULL) return head;
ListNode *f = head, *t = head;
int len = ;
while(t != NULL && t->next != NULL)
{
len++;
t = t->next;
}
int mv =len - k % len; //新的头结点在链表中的位置
f = head;
while(--mv)
{
f = f->next; //找到新的头结点的前一个结点
}
t->next = head; //尾巴连上最初的头结点
ListNode * newhead = f->next; //新的头结点
f->next = NULL; //新的尾部
return newhead;
}
};
【leetcode】Rotate List(middle)的更多相关文章
- 【leetcode】Rotate Image(middle)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- 【leetcode】Reorder List (middle)
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Partition List(middle)
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 【leetcode】Spiral Matrix(middle)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【leetcode】Next Permutation(middle)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【leetcode】Reverse Bits(middle)
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- 【leetcode】Surrounded Regions(middle)☆
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
随机推荐
- 第二章平稳时间序列模型——AR(p),MA(q),ARMA(p,q)模型及其平稳性
1白噪声过程: 零均值,同方差,无自相关(协方差为0) 以后我们遇到的efshow如果不特殊说明,就是白噪声过程. 对于正态分布而言,不相关即可推出独立,所以如果该白噪声如果服从正态分布,则其还将 ...
- HDU 2007
/*杭电ACM ID:2007*/ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int main() { int in1, in2 ...
- 1_mysql +DBA职业发展
MYSQL + DBA 职业发展 mysql :the world's most popular open source database 最流行的开源数据库 数据库世界 关系数据库(又称SQL数据库 ...
- Swift2.1 语法指南——泛型
原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...
- Swift2.1 语法指南——析构过程
原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...
- HDU 1232 并查集/dfs
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...
- iOS9 beta 请求出现App Transport Security has blocked a cleartext HTTP (http://)
iOS9 beta 请求出现App Transport Security has blocked a cleartext HTTP (http://) http://www.bubuko.com/in ...
- 2015安徽省赛 I.梯田
http://xcacm.hfut.edu.cn/problem.php?id=1213 set + 搜索 姐姐是用搜索+二分做的,效率要高很多 #include<iostream> #i ...
- mysql查看字段注释(帮助信息)指令
select column_name,column_comment from INFORMATION_SCHEMA.columns where table_name='my_table'; 或者 sh ...
- Javascript闭包——懂不懂由你,反正我是懂了
摘要:“如果你不能向一个六岁的孩子解释清楚,那么其实你自己根本就没弄懂.”好吧,我试着向一个27岁的朋友就是JS闭包(JavaScript closure)却彻底失败了. 越来越觉得国内没有教书育人的 ...