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. OpenCV 计算区域的内部参数

    对于一个区域,怎么进一步针对区域内部特征进行处理呢 ? 首先,我们要提取出来内部的某些特征才能说话,下面提取一些简单的特征,话不多说见代码: 1.平均数及方差参数: Mat tempMean, tem ...

  2. java:StringBuffer字符处理对象

    1.添加字符 public class StringBufferDemo { public static void main(String args[]) { StringBuffer sbf = n ...

  3. 2016年12月31日 学习java 第一天

    6个月没写代码了 现在从头开是学 又遇到了很基础的问题 以前配环境变量的时候  配过classpath  其实不要配classpath  因为运行的时候会优先去classpath去找 class文件  ...

  4. iBATIS代码生成分析

    1.分析目录结构"tools"(或者说模板) 2.首先分析ibatorConfig.xml文件 <?xml version="1.0" encoding= ...

  5. HTTP 错误 500.23 - Internal Server Error 检测到在集成的托管管道模式下不适用的 ASP.NET 设置。

    检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为[经典]模式). - CatcherX 2014-03-11 11:03 27628人阅读 评论(2) 收藏 举报  分类 ...

  6. poj2965 The Pilots Brothers' refrigerator

    题目链接:http://poj.org/problem?id=2965 分析:1.这道题和之前做的poj1753题目差不多,常规思路也差不多,但是除了要输出最少步数外,还要输出路径.做这道题的时候在怎 ...

  7. PHP控制div块大小和颜色的例子

    网站为了设计的更好看,会有很多的样式,而用php来控制样式很常见,无聊写了一个可以用于列表展示的样式,不喜忽喷. 1.先添加一个style样式控制div默认不换行 <style>div{f ...

  8. PHP PDO函数库详解

    PDO是一个“数据库访问抽象层”,作用是统一各种数据库的访问接口,与mysql和mysqli的函数库相比,PDO让跨数据库的使用更具有亲和力:与ADODB和MDB2相比,PDO更高效.目前而言,实现“ ...

  9. ARM+LINUX 项目学习总结

    一.确定功能 二.系统移植 1. 根据具体板子修改u-boot (三星的开发板资料) 2. 根据具体板子和功能修改内核 (基本的驱动) 3. 移植busybox 三.驱动修改编写 四.应用编程 附1 ...

  10. Linux系统的初化始配置(包括配置网络,修改主机名,关闭firewalld与selinux的生效)

    一.配置网络环境 1.运行 cmd 查看本机的ip地址,然后记录下来. 2.网络和共享中心--本地连接属性如下: 手工配置到本地连接上面 IP:172.16.191.215,DNS:101.7.8.9 ...