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 ...
随机推荐
- 【BZOJ】【1020】【SHOI2008】安全的航线flight
计算几何/二分/迭代/搜索+剪枝 写三个tag可能是因为从哪个方向来理解都可以吧…… 我完全不会计算几何所以抄了ydc的代码 题解:http://ydcydcy1.blog.163.com/blog/ ...
- iTunes Connect TERMS OF SERVICE
iTunes Connect TERMS OF SERVICE THESE TERMS OF SERVICE CONSTITUTE A LEGAL AGREEMENT BETWEEN YOU AND ...
- DSP中的cmd文件
一.CMD文件 链接命令文件(Link Command Files),以后缀.cmd结尾,简称CMD文件. CMD文件的两大功能是指示存储空间和分配段到存储空间. 在编写CMD文件时,主要采用MEMO ...
- 更改DEVExpress的Column的DisplayFormat为自定义的方法。
更改DEVExpress的Column的DisplayFormat为自定义的方法. public partial class Form1 : XtraForm { public Form1() { I ...
- C# 序列化 Serialize 的应用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- 141029 V7 异步通知测试,15分钟循环代码重构,同步更新payInfo测试,支付成功timer测试成功说明
支付成功之后的1分钟定时任务:测试成功. 2014-10-29 17:16:06,892 DEBUG [http-bio-8086-exec-1] org.springframework.web.se ...
- hadoop命令行命令
1. bin/hadoop fs -ls / 查看hdfs根目录下的文件 2. bin/hadoop fs -ls /user 查看user下文件 /就是根目录的意思.
- python笔记1
1.python中的语句块是用缩进表示,并不像C类语言中用{}表示语句块,还有就是语句块的开始貌似是用:表示,然后C类语言中()在python中用"空格"表示了,例如python中 ...
- ZOJ3550 Big Keng(三分)
题意:给定一个立体的图形,上面是圆柱,下面是圆台,圆柱的底面半径和圆台的上半径相等,然后体积的V时,问这个图形的表面积最小可以是多少.(不算上表面).一开始拿到题以为可以YY出一个结果,就认为它是圆锥 ...
- java 反射创建对象并传入参数
/* * 通过反射创建带参数的对象 */ public Object Creatobject(String ClassPath, Object[] Params) throws Exception { ...