题目

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 andk=2,

return 4−>5−>1−>2−>3−>NULL.

分析

给定一个链表,以及一个整数k,返回链表右旋k个元素后的结果。

要使得链表右旋k个元素,也就是说明链表后(k)个元素将成为新链表的前半部分,原链表的前(len−k)个元素将成为新链表的后半部分;

此时原链表的head前恰好有k 个元素,即完成了右旋k个位置。

要注意的是,当k=0||n∗len时,原链表将不变,只有当k的结果为 1 len−1时,链表才会发生变化。

AC代码

/**
* 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)
return head; ListNode *p = head; //求链表的长度
int len = 0;
while (p)
{
len++;
p = p->next;
} k %= len; //k<=0时,原链表不旋转
if (k <= 0)
return head; int index = 1;
//寻找右旋k位置后,链表的首结点
p = head;
while (index < (len - k) && p->next != NULL)
{
index++;
p = p->next;
} ListNode *ret = p->next, *q = p; //原链表寻找尾结点,将其链接到head
while (p->next)
p = p->next;
p->next = head; //前部分尾结点设为NULL
q->next = NULL;
return ret; }
};

GitHub测试程序源码

LeetCode(61) Rotate List的更多相关文章

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

  2. LeetCode(61):旋转链表

    Medium! 题目描述: 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, ...

  3. LeetCode(48)Rotate Image

    题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise ...

  4. Qt 学习之路 2(61):使用 SAX 处理 XML

    Qt 学习之路 2(61):使用 SAX 处理 XML  豆子  2013年8月13日  Qt 学习之路 2  没有评论 前面两章我们介绍了使用流和 DOM 的方式处理 XML 的相关内容,本章将介绍 ...

  5. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  6. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  7. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  8. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  9. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

随机推荐

  1. ————————C语言中快速排序方法——————————————

    在对浮点型排序是一定要用三木运算符(三目运算符内容下去自己看),因为如果也是用整形那样的减法的时候如果是两个十分相近的数字 可能返回一个小数(自己一会去试试),冉冉他cmp返回值是int(试试别的)因 ...

  2. java基础类型数据与String类包装类之间的转换与理解

    数据类型转换一般分为三种: 在java中整型,实型,字符型视为简单数据类型,这些数据类型由低到高分别为:(byte,short,char--int-long-float-double) 简单数据类型之 ...

  3. [转]广义正交匹配追踪(gOMP)

    广义正交匹配追踪(Generalized OMP, gOMP)算法可以看作为OMP算法的一种推广,由文献[1]提出,第1作者本硕为哈工大毕业,发表此论文时在Korea University攻读博士学位 ...

  4. [POI2012]Vouchers

    Description 考虑正整数集合,现在有n组人依次来取数,假设第i组来了x人,他们每个取的数一定是x的倍数,并且是还剩下的最小的x个. 正整数中有m个数被标成了幸运数,问有哪些人取到了幸运数. ...

  5. 暑期训练狂刷系列——Hdu 1698 Just a Hook (线段树区间更新)

    题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1698 题目大意: 有一个钩子有n条棍子组成,棍子有铜银金三种组成,价值分别为1,2,3.为了对付每场 ...

  6. 小心我“DIR”溢出你!

    转自https://blog.csdn.net/wql19881207/article/details/6300760 https://blog.csdn.net/wql19881207/articl ...

  7. AJPFX区分this和super

    this和super的区别No.区别thissuper1操作属性this.属性:表示调用本类中的属性,如果本类中的属性不存在,则从父类查找super.属性:表示调用父类中的属性2操作方法this.方法 ...

  8. 关于java的arrays数组排序示例AJPFX的分享

    Java API对Arrays类的说明是:此类包含用来操作数组(比如排序和搜索)的各种方法. 1.对基本数据类型的数组的排序 说明: (1)Arrays类中的sort()使用的是“经过调优的快速排序法 ...

  9. Bmob使用心得

    1.在 Project 的 build.gradle 文件中添加 Bmob的maven仓库地址,示例如下:(注意文字说明部分): allprojects { repositories { jcente ...

  10. AlertDialog的实现

    课程Demo 重点解析自定义对话框 public class MainActivity extends AppCompatActivity { private Button bt1; private ...