leetcode:Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.
For example:
Given 1->2->3->4->5->NULL
and k = 2
,
return 4->5->1->2->3->NULL
.
分析:题意为将一个链表向右旋转k位。
思路:此题同样可以利用双指针,第一个指针从头开始向后移动k位,然后第二个指针指向头指针,接着两个指针一起向后移动直到第一个指针指向尾节点。
建立第三个辅助指针指向第二个指针的后继结点作为将要返回的新头指针,再把第二个指针的后继设为空指针,同时将第一个指针的后继指向原先的头指针,这样就能完成旋转啦!
代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *rotateRight(ListNode *head, int k)
{
if(head == NULL || k <= 0)
return head; ListNode *temp = head;
int count = 0;
while(temp != NULL)
{
count++;
temp = temp->next;
} if(k > count)
k = k%count;
if(k == count || k == 0)
return head; ListNode *first = head;
while(k > 0)
{
first = first->next;
k--;
} ListNode *second = head;
while(first->next != NULL)
{
first = first->next;
second = second->next;
} ListNode *newhead = second->next;
first->next = head;
second->next = NULL;
return newhead;
} };
leetcode:Rotate List的更多相关文章
- 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 ...
- [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】 Rotate List 循环链表
题目:rotate list 解法1: <span style="font-size:18px;">/**LeetCode Rotate List:Given a li ...
- LeetCode:旋转图像【48】
LeetCode:旋转图像[48] 题目描述 给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使 ...
- [LeetCode] 189. Rotate Array 旋转数组
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- [LeetCode] 61. Rotate List 旋转链表
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
- leetcode:Roman to Integer(罗马数字转化为罗马数字)
Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...
- Java [Leetcode 189]Rotate Array
题目描述: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ...
- LeetCode(67)-Rotate Array
题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ar ...
随机推荐
- SQL Server优化
虽然查询速度慢的原因很多,但是如果通过一定的优化,也可以使查询问题得到一定程度的解决. 查询速度慢的原因很多,常见如下几种: 没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) I/ ...
- ios containerViewController
- (void)replaceViewController:(UIViewController *)existingViewController withViewController:(UIViewC ...
- ios 调用打印机
源码 无意中玩一个demo发现调用了打印机 才发现ios有快速调用打印机的功能. if ([UIPrintInteractionController isPrintingAvailable] == ...
- svn 分支与合并的使用
在使用svn的时候我们往往有这样的需求.我们修改某些代码,因为对某项技术不是非常的熟悉,担心自己当前的修改(或者叫测试)会影响到服务器中版本库代码的崩溃等.传统做法我们会手动复制一份代码,然后修改 ...
- Unity 3D 游戏上线之后的流水总结
原地址:http://tieba.baidu.com/p/2817057297?pn=1 首先.unity 灯光烘焙 :Unity 3D FBX模型导入.选项Model 不导入资源球.Rig 不导入骨 ...
- JSP 页面打印
<HTML><HEAD><TITLE>javascript打印-打印页面设置-打印预览代码</TITLE> <META http-equiv=Co ...
- POJ 2100
Graveyard Design Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 4443 Accepted: 946 ...
- Linux下搭建Android NDK , Linux 驱动开发环境
Eclispe Luna(4.4):http://www.eclipse.org/downloads/ CDT :http://www.eclipse.org/cdt/downloads.php AD ...
- 妙味课堂——HTML+CSS(第四课)(二)
单开一篇来讲一个大点的话题——清浮动 来看下例: <!DOCTYPE html> <html> <head> <meta charset="U ...
- lintcode:装最多水的容器
装最多水的容器 给定 n 个非负整数 a1, a2, ..., an, 每个数代表了坐标中的一个点 (i, ai).画 n 条垂直线,使得 i 垂直线的两个端点分别为(i, ai)和(i, 0).找到 ...