【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 ...
随机推荐
- 【MongoDB】MongoDb的“not master and slaveok=false”错误及解决方法
链接mongodb报错如下 2016-03-14T16:26:00.912+0800 E QUERY [thread1] Error: listDatabases failed:{ "ok& ...
- ASP跨域调用Webservices方法
仅用于记录与分享,直接贴代码: <script type="text/javascript"> function check(){ var title=$('#titl ...
- 滑雪 why WA
滑雪 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 587 Solved: 219 Description 小明喜欢滑雪,因为滑雪的确很刺激,可是为了获 ...
- OS X Framework Library not loaded: 'Image not found'的解决办法
参考:OS X Framework Library not loaded: 'Image not found' 1.首先将相应的framework手动复制到/System/Library/Framew ...
- XH
1. 又到父亲节,那就给老爹做顿饭呗,让他开心开心. 老爸吃了一口我炒的菜,流露出感动的泪花说:儿呀,你能为爸爸做饭,爸爸感到特别开心,但是你这个菜,看在今天是父亲节 我能不能不吃呀! 2. 一哥 ...
- nginx 虚拟主机
基于域名的虚拟主机 创建站点目录 [root@nginx conf]# cd /usr/local/nginx/html/ [root@nginx html]# pwd /usr/local/ngin ...
- iOS开发——UI进阶篇(十一)应用沙盒,归档,解档,偏好设置,plist存储,NSData,自定义对象归档解档
1.iOS应用数据存储的常用方式XML属性列表(plist)归档Preference(偏好设置)NSKeyedArchiver归档(NSCoding)SQLite3 Core Data 2.应用沙盒每 ...
- iOS开发——UI进阶篇(八)pickerView简单使用,通过storyboard加载控制器,注册界面,通过xib创建控制器,控制器的view创建,导航控制器的基本使用
一.pickerView简单使用 1.UIPickerViewDataSource 这两个方法必须实现 // 返回有多少列 - (NSInteger)numberOfComponentsInPicke ...
- JQuery的Ajax跨域请求的解决方案
客户端调用代码示例: var myurl = "http://js.yingdoo.com/embed/CAPTCHA.ashx?m=" + phone_val + "& ...
- IDEA 编译找不到符号,文件却没有错误。
单独编译提交找不到符号的文件. DIEAA