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的更多相关文章

  1. 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  ...

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

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

  3. 【LeetCode】 Rotate List 循环链表

    题目:rotate list 解法1: <span style="font-size:18px;">/**LeetCode Rotate List:Given a li ...

  4. LeetCode:旋转图像【48】

    LeetCode:旋转图像[48] 题目描述 给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使 ...

  5. [LeetCode] 189. Rotate Array 旋转数组

    Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...

  6. [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 ...

  7. leetcode:Roman to Integer(罗马数字转化为罗马数字)

    Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. java 验证身份证号

  2. [工作积累] GCC 4.6 new[] operator内存对齐的BUG

    对于用户没有定义dctor(包括其所有成员)的类来说, new CLASS[n] 可能会直接请求sizeof(CLASS)*n的空间. 而带有dctor的 类, 因为delete[]的时候要逐个调用析 ...

  3. Sublime搭建nodejs环境(windows)

    1.下载nodejs,并安装ok后,配置好环境变量. 2.下载sublime text3 3.在package install 包中新增node插件(或者直接去SublimeText-Nodejs插件 ...

  4. [剑指OFFER] 数组中的逆序对

    题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数.     分析:利用归并排序的思想,分成2部分,每一部分按照从大到 ...

  5. WPF 验证

    WPF中TextBox的自动验证: 演示 : 用以下两个TextBox分别显示验证IP和非空值验证,先看效果: IP自动验证效果: 非空值自动验证效果: 第一步:定义TextBox验证的样式: < ...

  6. ios 分类(Category)

      今天研究了类别,都是网上找的资料,类别的作用 类别主要有3个作用:       (1)将类的实现分散到多个不同文件或多个不同框架中.       (2)创建对私有方法的前向引用.       (3 ...

  7. Notepad++中常用的插件

    Notepad++中常用的插件 Notepad++实用插件分享 otepad++前端开发常用插件介绍

  8. Sqli-labs less 18

    Less-18 本关我们这里从源代码直接了解到 对uname和passwd进行了check_input()函数的处理,所以我们在输入uname和passwd上进行注入是不行的,但是在代码中,我们看到了 ...

  9. zoj Fibonacci Numbers ( java , 简单 ,大数)

    题目 //f(1) = 1, f(2) = 1, f(n > 2) = f(n - 1) + f(n - 2) import java.io.*; import java.util.*; imp ...

  10. POJ 1679 The Unique MST(次小生成树)

    题意:求解最小生成树的权值是否唯一,即要我们求次小生成树的权值两种方法求最小生成树,一种用prim算法, 一种用kruskal算法 一:用prim算法 对于给定的图,我们可以证明,次小生成树可以由最小 ...