题目

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. X86 Linux 下 SIGBUS 总结

    SIGBUS 在 x86 Linux 上并不多见,但一旦出现,其调用堆栈常常让人摸不着头脑,加之信号问题各平台系统间差异较大,更让人难以理清,这里稍微总结一下 x86 Linux 上大概有哪些情形会触 ...

  2. 机器学习基础算法__python实现(基于numpy等基础库)

    # 博客转自https://blog.csdn.net/weixin_39561100/article/details/80879211 主要是将<机器学习实战>中的算法实现一遍,后续会慢 ...

  3. 模拟 hihoCoder 1177 顺子

    题目传送门 /* 模拟:简单的照着规则做就可以了,把各种情况考虑到,虽然比赛写的丑了点,但能1Y还是很开心的:) */ #include <cstdio> #include <cst ...

  4. 动态规划:最大连续子序列乘积 分类: c/c++ 算法 2014-09-30 17:03 656人阅读 评论(0) 收藏

    题目描述: 给定一个浮点数序列(可能有正数.0和负数),求出一个最大的连续子序列乘积. 分析:若暴力求解,需要O(n^3)时间,太低效,故使用动态规划. 设data[i]:第i个数据,dp[i]:以第 ...

  5. hibernate 中createQuery与createSQLQuery(转载)

    息: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.miracle.dm.doc.catalog.mo ...

  6. CoreText的绘制流程-转

    来自:http://blog.sina.com.cn/s/blog_7c8dc2d50101lbb1.html 使用coreText进行文本绘制,需要在工程中添加CoreText.framework, ...

  7. APP多渠道打包

    多渠道打包的概念: 打包是指使用证书文件对app签名生成一个apk文件. 多渠道打包指的就是我们的app在开发完成之后需要投放到不同的市场,比如说Google市场.百度市场等,为了统计应用在各个市场的 ...

  8. 动态生成li标签,并设置点击事件

    今天要解释的是如下界面              主要实现了: 1.模拟后台的json数据,动态生成li标签 2.导航栏的下划线 3.给li标签右边设置图片 4.动态生成的li标签,设置选中的li的点 ...

  9. 设置webbrowser浏览器内核

    var hklm = Microsoft.Win32.Registry.LocalMachine;            var lmRun64 = hklm.OpenSubKey(@"SO ...

  10. pickle序列化与反序列化 + eval说明

    import pickle # #1.从文件中读取pickle格式with open('egon.json','rb') as f: pkl=f.read()#2.将json_str转成内存中的数据类 ...