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的更多相关文章

  1. C++ STL@ list 应用 (leetcode: Rotate Array)

    STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...

  2. [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  ...

  3. [LeetCode] Rotate List 旋转链表

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  4. [LeetCode] Rotate Image 旋转图像

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  5. LeetCode——Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given  ...

  6. [LeetCode]Rotate Image(矩阵旋转)

    48. Rotate Image     Total Accepted: 69437 Total Submissions: 198781 Difficulty: Medium You are give ...

  7. [leetcode]Rotate List @ Python

    原题地址:https://oj.leetcode.com/problems/rotate-list/ 题意: Given a list, rotate the list to the right by ...

  8. [leetcode]Rotate Image @ Python

    原题地址:https://oj.leetcode.com/problems/rotate-image/ 题意: You are given an n x n 2D matrix representin ...

  9. 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 = ...

  10. [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 ...

随机推荐

  1. 火狐浏览器 js 1到9月份 new DATE不返回时间

    new Date('2016-1'); //错误 1到9月份必须 01 02 ...... 正确 new Date('2016-01'); var nowMonth = nowMonth>=10 ...

  2. [ubuntu]用ubuntu开发的日子----win7 ubuntu双系统

    小子终于忍不了win7某些蛋疼的设定,看群里好多大牛推荐mac,但资金紧张,只好推而求其次使用ubuntu,但是由于公司工作环境是windows,所以还必须保留windows系统,一次决定双系统. 下 ...

  3. virtualbox虚拟机中的centos与macos共享文件夹

    开发中需要用到linux环境,所以使用共享模式开发.通过samba服务器来实现. 环境: 虚拟机 virtualbox 虚拟系统 centos 6.6 本机 macos  192.168.1.102 ...

  4. android Glide图片加载框架的初探

    一.Glide图片加载框架的简介 谷歌2014年开发者论坛会上介绍的图片加载框架,它让我们在处理不管是网路下载的图片还是本地的图片,减轻了很多工作量, 二.开发步骤: 1.添加链接库 compile ...

  5. ActiveReports中自定义Winforms的Viewer 工具栏

    ActiveReports中提供不同平台的报表浏览器来加载报表,而不同平台的报表浏览器功能也不一致,今天我们来学习如何定制Winforms Viewer控件. 预览效果: 核心代码: C# // C# ...

  6. python——进程基础

    我们现在都知道python的多线程是个坑了,那么多进程在这个时候就变得很必要了.多进程实现了多CPU的利用,效率简直棒棒哒~~~ 拥有一个多进程程序: #!/usr/bin/env python #- ...

  7. Ubuntu Server 配置网络

    ------------------ Ubuntu 14.04   x86_64 ----------------- 设置静态IP:vi /etc/network/interfaces. 然后再编辑 ...

  8. chrome经常崩溃解决过程

    之前chrome常崩溃,也没有找到原因,就将就着用吧,一直用到今天,今天连续几次崩了,突然想到,难道是因为我访问的域名没有解析(能想到这个,是因为今天分配公司域名测试的时候常输错),于是就输入一个不存 ...

  9. MVC3 新建项目

    一.安装工具 二.新建项目 step1:新建MVC3项目 打开新建项目窗口,在“已安装的模板”列表中选择“Web”,在右侧应用程序模板列表中选择“ASP.NET MVC3 Web应用程序”,修改项目名 ...

  10. Rank() 、DENSE_RANK()、NTILE(n)的用法-转

    Rank() over()/DENSE_RANK()  over()的用法 1.Rank() over()/DENSE_RANK()  over() 这两个函数与ROW_NUMBER()函数类似,因为 ...