LeetCode Rotate List
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k)
{
int level = -1;
int size = 0;
ListNode *ptr = head;
while(ptr != nullptr) //一定要找到size,并取余,不然这个算法就是错的
{
size++;
ptr = ptr->next;
}
if(size == 0) return nullptr;
cout << k%size<<endl;
ListNode *newhead = findNewHead(head, k%size, level);
ptr = head;
while( ptr != nullptr)
{
if(ptr->next == newhead) ptr->next = nullptr; //要将新的头节点的前前节点设置为nullptr
ptr = ptr->next;
}
ptr = newhead;
while(ptr != nullptr && ptr->next != nullptr && ptr != head) // Error: 要找到最后一个,再连接到head
ptr = ptr->next;
if(ptr!= nullptr && ptr->next == nullptr && ptr != head) ptr->next = head;//Error:要判断ptr是不是head,不然容易产生环状
if(newhead == nullptr) newhead = head; //newhead返回nullptr时如何处理也非常关键
return newhead;
}
ListNode* findNewHead(ListNode * node, int k, int& level)
{
ListNode* newhead = nullptr;
if(node == nullptr) {level = 0; return nullptr;}
if(level < 0)
newhead = findNewHead(node->next, k, level);
if(level == k)
{
return newhead; //哪条路径返回什么值也一定要搞清楚
}
if(level >= 0) level++;
return node; //哪条路径返回什么值也一定要搞清楚
}
};
LeetCode Rotate List的更多相关文章
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
- [LeetCode] Rotate Array 旋转数组
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- [LeetCode] Rotate List 旋转链表
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- [LeetCode] Rotate Image 旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- LeetCode——Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...
- [LeetCode]Rotate Image(矩阵旋转)
48. Rotate Image Total Accepted: 69437 Total Submissions: 198781 Difficulty: Medium You are give ...
- [leetcode]Rotate List @ Python
原题地址:https://oj.leetcode.com/problems/rotate-list/ 题意: Given a list, rotate the list to the right by ...
- [leetcode]Rotate Image @ Python
原题地址:https://oj.leetcode.com/problems/rotate-image/ 题意: You are given an n x n 2D matrix representin ...
- 2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe
Rotate Array 本题目收获: 题目: Rotate an array of n elements to the right by k steps. For example, with n = ...
- [LeetCode] Rotate Function 旋转函数
Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotati ...
随机推荐
- ios视图frame和bounds的对比
bounds坐标:自己定义的坐标系统,setbound指明了本视图左上角在该坐标系统中的坐标, 默认值(0,0) frame坐标: 子视图左上角在父视图坐标系统(bounds坐标系统)中的坐标, ...
- 【第1期】腾讯云的1001种玩法征集,Ipad mini和Kindle 等你拿!(文章评审中)
版权声明:本文由阁主的小跟班原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/695994001482226944 来源:腾云 ...
- NGUI Atlas Maker sprites with black line issue
NGUI图集中的图,在游戏中显示出来带有黑边的问题. 实际上是因为图片在导入到图集中,图片四周的完全透明的边缘部分会被裁掉,而在图集中的实际大小比图片原始大小小以及图集中图片之间的间距设置得太小导致. ...
- python SimpleHTTPRequestHandler初探
1,转自 https://blog.gtwang.org/web-development/python-simplehttpserver-web-server/ 如果你急需一个简单的Web Serve ...
- acm系统开发笔记
时间: 2016/2/29 遇到的困难: 数据库配置的mysql和java(Date)不一致,出现下面错误 Date date = new Date(); SimpleDateFormat ...
- SharePoint 2013 List 备份使用
在测试环境新建List后经过不懈的调整,验证终于做出一个像模像样的表单. 这时候问题来... 要怎么迁移到生产环境或者正式环境呢? 在网上找了一些资料,不过都是10的.. 其实想想13跟10区别不大, ...
- SQL Developer报错:Unable to find a Java Virtual Machine解决办法
安装了64位的Oracle数据库以及32位的Oracle客户端,在开始菜单中第一次打开客户端的SQL Developer时提示输入java.exe的路径,我选择了Oracle数据库自带的jdk路径,确 ...
- MySql学习(二) —— where / having / group by / order by / limit 简单查询
注:该MySql系列博客仅为个人学习笔记. 这篇博客主要记录sql的五种子句查询语法! 一个重要的概念:将字段当做变量看,无论是条件,还是函数,或者查出来的字段. select五种子句 where 条 ...
- background复合属性详解(上):background-image
background复合属性是个很复杂的属性,花样非常多,比较神奇的是css3 中支持多图片背景了,这篇文章先讲讲background-image属性,其他背景属性会在后续的文章综合总结. 一.最基本 ...
- JAVA数据转换常用方法
时间格式化与运算 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar calendar=sdf. ...