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. 不在折腾---storm-0.9.2-incubating分布式安装

    安装一个zookeeper集群 > 请参考:不在折腾----zookeeper-3.4.5 上传strom的安装包 解压 配置,conf/storm.yaml * 所使用的zookeeper集群 ...

  2. java通过文件路径读取该路径下的所有文件并将其放入list中

    java通过文件路径读取该路径下的所有文件并将其放入list中   java中可以通过递归的方式获取指定路径下的所有文件并将其放入List集合中.假设指定路径为path,目标集合为fileList,遍 ...

  3. 20169212《Linux内核原理与分析》 第十周作业

    云课堂回顾学习 1. 进程调度的时机 中断处理过程(包括时钟中断.I/O中断.系统调用和异常)中,直接调用schedule(),或者返回用户态时根据need_resched标记调用schedule() ...

  4. 【转】一种解决h5页面背景音乐不能自动播放的方案

    原文:http://www.cnblogs.com/wmhuang/p/5452259.html --------------------------------------------------- ...

  5. MEF的学习笔记

    为什么要使用MEF 在商业应用软件开发过程中,对于各个软件项目,都需要建立相应的系统框架,为了更好的规范系统的开发,提高生产效率,应该在公司级别制定相应的API标准.这些API标准将站在系统架构层次, ...

  6. css中的一些属性解析

    1.inline-block 存在问题:inline-block的相互间距,元素之间会有一个左右2px的margin一样产生            请看中间的空隙. 为什么会产生这个空隙呢?? 怎么解 ...

  7. JavaScript 面向对象(二) —— 案例篇

    看案例前可以先看看基础篇:JavaScript 面向对象(一) —— 基础篇 案例——面向对象的选项卡:把面向过程的程序一步步改成面向对象的形式,使其能够更加的通用(但是通用的东西,一般会比较臃肿). ...

  8. JQuery,拼接字符串问题(求助)

    Js代码 $("#span_btnSave").html(str1); 结果 <span id="span_btnSave"><button ...

  9. PDF 补丁丁 0.5.0.2713 发布(替换字库功能修正字符宽度问题)

    新版本替换字库后,采用新字库的字符宽度.基本上可以满足一般的字库替换需求.请下载新版本测试.

  10. Apache AB 如何传递参数

    AB使用时,网上通篇一律,在进行示例时使用的连接一般都是http://*.com,这种写法是没有带参数,如果你想测试一个写入的Case,那需要传递参数给后台,如何传递参数呢? 这里有一个登录的请求,需 ...