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. 数据库查询优化-SQL优化

    1.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如:select id from t where num is null可以在num上设置默 ...

  2. plist基本操作

    重要概念:某些路径下“只能读,不能写”的原因 iPhone.ipad真机上 Resouces文件夹:是只读的,无法写入. document 和temp文件夹:可读,可写. 一.工程结构

  3. 不注册Tomcat服务,运行Tomcat不弹出JAVA控制台窗口

    http://blog.csdn.net/yangholmes_blog/article/details/52881296

  4. Discuz论坛安全加固浅析

    [51CTO专稿]Discuz! 论坛以其功能完善.效率高效.负载能力,深受被大多数的网站喜爱和青睐.无独有隅,笔者所维护的论坛就是用discuz! 来构建的,从接手时候的7.2到现在x2.0,经历了 ...

  5. WordPress登陆页和后台面空白解决方法

    真没想到我居然也会碰到这么蛋疼的事情,有一天我登陆博客,输入账号密码之后登陆没有反应,之后我就试着用首页前台登陆(因为这个模板前台带登陆功能),之后成功登陆进入后台更新文章.我想算了.这小毛病就丢那吧 ...

  6. cookie多次点赞效果

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  7. git使用和理解之一(不含分支)

    0.前言 Workspace:工作区 Index / Stage:暂存区 Repository:仓库区(或本地仓库) Remote:远程仓库 工作区和暂存区: 我们写代码的地方就是工作区,代码写完后, ...

  8. vba单元格背景色

    cells(i,j).interior.color=5287936  green =255 red

  9. Transport Block Size, Throughput and Code rate-----http://www.simpletechpost.com/2012/12/transport-block-size-code-rate-protocol.html

    Transport Block Size, Throughput and Code rate   Since the size of transport block is not fixed, oft ...

  10. GOLDENGATE 配置文档,各类参数--转发

    1       GoldenGate简要说明 GoldenGate现在是业内成熟的数据容灾与复制产品,经过多年的发展与完善,现在已经成为业内事实上的标准之一. GoldenGate软件是一种基于日志的 ...