http://oj.leetcode.com/problems/rotate-list/

取得后面k个节点,然后截断插到前面。如果k比list长,则按照求余算。

去后面的k个节点:使用两个指针,第一个指针比第二个指针先走k步,然后两个一起往后走,等到第一个到达最后一个节点,第二个就是倒数第k个节点。

#include <iostream>
using namespace std; struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}; class Solution {
public:
ListNode *rotateRight(ListNode *head, int k) {
//找到倒数第k个节点
ListNode *pEnd = head,*pLastK = head;
int _k = k;
if(k== ||head == NULL)
return head; //计算共有多少个节点
ListNode *node_count = head;
int count = ;
while(node_count)
{
node_count = node_count->next;
count++;
}
if(_k>count)
_k = k%count;
if(_k == )
return head; while(_k--)
{
if(pEnd == NULL)
return head;
pEnd = pEnd->next;
}
if(pEnd == NULL)
return head;
while(pEnd->next)
{
pEnd = pEnd->next;
pLastK = pLastK->next;
}
//截断
ListNode *ans = pLastK->next;
pLastK->next = NULL;
//再插到前面
ListNode *temp = ans;
while(temp->next)
{
temp = temp->next;
}
temp->next = head;
return ans;
}
}; int main()
{
Solution myS;
ListNode *n1 = new ListNode();
ListNode *n2 = new ListNode();
ListNode *n3 = new ListNode();
ListNode *n4 = new ListNode();
ListNode *n5 = new ListNode();
n1->next = n2;
n2->next = n3;
n3->next = n4;
n4->next = n5;
myS.rotateRight(n4,);
return ;
}

LeetCode OJ--Rotate List的更多相关文章

  1. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  2. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  3. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  4. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  5. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  6. [array] leetcode - 48. Rotate Image - Medium

    leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...

  7. 备份LeetCode OJ自己编写的代码

    常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...

  8. LeetCode OJ 之 Maximal Square (最大的正方形)

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  9. LeetCode OJ:Integer to Roman(转换整数到罗马字符)

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  10. LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

随机推荐

  1. [BZOJ] 1441 Min

    题意:给一堆数ai,求S=Σxiai,使得S最小且为正整数 根据裴蜀定理,一定存在ax+by=gcd(a,b),同理可以推广到n个整数 也就是说,在不考虑正负的情况下,所有数的gcd就是所求 #inc ...

  2. 转 消息队列之 RabbitMQ

    转 https://www.jianshu.com/p/79ca08116d57 消息队列之 RabbitMQ 预流 2017.05.06 16:03* 字数 4884 阅读 80990评论 18喜欢 ...

  3. 将远程分支拷贝到本地,并更新代码push到原分支

    第一步:git clone +主分支 第二步:git fetch origin 分支名 第三步:git checkout -b 分支名 origin/分支名 第四步:git pull origin 分 ...

  4. destoon 列表页面增加手动选择排序方式

    在mobile/include/mall.inc.php  行60  $order = $MOD['order']; 之前增加 排序方式判断 如果有order参数则$order接受参数,没有就用默认  ...

  5. Python入门基本语法

      Python入门 以下主要讲述Python的一些基础语法,包含行的缩进在python中的重要意义,python中常见的保留字和引号的使用,如何实现单行注释和多行注释. print("he ...

  6. Python中变量的作用域

    一.变量作用域的含义 变量的作用域说白了就是变量的值从哪里获取,或者说变量取值的地方 我们在写代码过程中会用到很多变量,这些变量会出现在各种代码块中,有的出现在函数块里,有的在函数块外,例如: def ...

  7. P3391 【模板】文艺平衡树FHQ treap

    P3391 [模板]文艺平衡树(Splay) 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转 ...

  8. 3224: Tyvj 1728 普通平衡树(新板子)

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 17048  Solved: 7429[Submit][St ...

  9. python 模块相互import

    模块A中import B,而在模块B中import A.这时会怎么样呢?这个在Python列表中由RobertChen给出了详细解释,抄录如下: [A.py] from B import D clas ...

  10. JS页面快捷键库hotkeys.js

    网友提供了一个好用的快捷键库,没有任何依赖,这是一个强健的 Javascript 库用于捕获键盘输入和输入的组合键,它没有依赖,压缩只有只有(~3kb). 这里也要特别感谢园友kacper的提醒与提供 ...