LeetCode OJ--Rotate List
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的更多相关文章
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- LeetCode OJ学习
一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...
- 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 ...
- [array] leetcode - 48. Rotate Image - Medium
leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...
- 备份LeetCode OJ自己编写的代码
常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Ubuntux下简单设置vim
我自己在vim下的设置,基本写简单脚本用的,在~/.vimrc作出如下设置 syntax on "高亮 set nu "行号显示 set smartindent "基于a ...
- 【laravel】laravel class 里面定义以head开头的方法会报错
BadMethodCallException in Macroable.php line 81:Method head does not exist.
- redis集群监控之Redis-monitor部
为了对以后有可能面临的redis集群监控做准备,这两天在准备这方面的事情,现在将其中的过程记录一下. 首先是“Ronney-Hua”的这篇文章对三中开源监控软件做了对比 文章地址:https://bl ...
- 在Unix系统上,从源文件、目标文件、可执行文件的编译过程
是由“编译器驱动”(compiler driver)完成的: unix> gcc -o hello hello.c 在这里,gcc的编译器驱动程序读取源文件hello.c, #include & ...
- k短路模板
https://acm.taifua.com/archives/jsk31445.html 链接: https://nanti.jisuanke.com/t/31445 #include <io ...
- 20,序列化模块 json,pickle,shelve
序列化模块 什么叫序列化? 将原本的字典,列表等内容转换成一个字符串的过程叫做序列化. 序列化的目的? 数据结构 通过序列化 转成 str. str 通过反序列化 转化成数据结构. json: jso ...
- DataContext.ExecuteQuery的两种方法调用
ExecuteQuery主要用于DataContext类直接执行SQL语句的查询,在MSDN上有两种执行方法,下面为两种方法的不同调用: 1.ExecuteQuery<TResult>(S ...
- iphone使用keychain来存取用户名和密码
1.在arc下系统提示使用__bridge http://www.cnblogs.com/zzltjnh/p/3885012.html 参考文档:http://blog.csdn.net/jerr ...
- 教你玩App怎么赚钱(一)
在看这篇文章之前,你一定要接受一下谋哥的观点:金钱就是价值流通的手段,不要高看了钱. 玩App怎么赚钱?貌似谋哥写的文章超级多,把这个最重要的忘记了.说实在的,我为啥要写“玩App"呢?其实 ...
- PHP-7.1 源代码学习:字节码在 Zend 虚拟机中的解释执行 之 概述
本文简要介绍 zend 虚拟机解释执行字节码的基本逻辑以及相关的数据结构,关于 PHP 源代码的下载,编译,调试可以参考之前的系列文章 execute_ex 我们来看看执行一个简单的脚本 test.p ...