题目

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. 【OpenJ_Bailian - 2945】拦截导弹(动态规划)

    拦截导弹  Descriptions: 某国为了防御敌国的导弹袭击,开发出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高 ...

  2. Luogu P1462 通往奥格瑞玛的道路【二分/最短路】

    题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡奥格瑞玛 题目描述 在艾泽拉斯, ...

  3. 配置yum源的步骤(阿里源)

    配置yum源的步骤1.可以移除默认的yum仓库,也就是删除 /etc/yum.repos.d/底下所有的.repo文件(踢出国外的yum源) 1.配置yum源,找到阿里云的官方镜像源地址 https: ...

  4. Lightoj 1054 - Efficient Pseudo Code

    题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1054 题目大意: 给出n,m,问n^m的所有因子之和是多少? 解题思路: 补 ...

  5. B - Archer

    Problem description SmallR is an archer. SmallR is taking a match of archer with Zanoes. They try to ...

  6. 题解报告:hdu 2069 Coin Change(暴力orDP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2069 Problem Description Suppose there are 5 types of ...

  7. Oracle报错:“ORA-18008: 无法找到 OUTLN 方案 ”的解决方案

    Oracle报错:“ORA-18008: 无法找到 OUTLN 方案 ”的解决方案   2.修改replication_dependency_tracking参数 SQL> alter syst ...

  8. DotNteBar 控件操作

    DotNteBar中ComboBoxEx.DroppedDown = true可以不点击该控件就显示其内容.将DropDownStyle属性设为DropDownList下拉框就不能进行编辑只能选择下拉 ...

  9. hbase rpc这点事

    年前的时候系统梳理了一下hbase rpc的实现,并且对组里的小伙伴做了一次分享.趁着热乎劲还没完全消失殆尽,准备赶紧记录下来. hbase中rpc概况 作为一个分布式系统,hbase的设计是典型的m ...

  10. mysql提升效率

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...