题目

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. Win7下安装MongoDB4.0.10

    前言 MySQL与MongoDB都是开源的常用数据库,但是MySQL是传统的关系型数据库,MongoDB则是非关系型数据库,也叫文档型数据库,是一种NoSQL的数据库.它们各有各的优点,关键是看用在什 ...

  2. java String, StringBuffer ,StringBulider 区别

    1. String 是不可变的(immutable): 创建后不能修改: 2. StringBuffer 线程安全的,可变字符串: 3. StringBuilder 线程不安全,所以性能比较高

  3. javascript:void(0)与#区别

    javascript:void(0)   鼠标点击时,不会跳转到其他页面,且停留在原地 #   鼠标点击时,不会跳转到其他页面,但会回到顶部

  4. PHP 官方说明

    http://php.net/manual/en/mysqli.affected-rows.php The above examples will output: Affected rows (INS ...

  5. myeclipse 跟踪struts 源码失败

    解决办法: 找到工程jar包所在的位置,点击右键:properties 点击external folder 找到  这个包下的src文件夹 导入之后, 源码会变色

  6. xmind8 Mac破解版(思维导图) 附序列号

    链接: https://pan.baidu.com/s/1PNdLRGpz_jhfPmWAIbLRfw 提取码: ruvm 复制这段内容后打开百度网盘手机App,操作更方便哦 小伙伴们XMind 8 ...

  7. Youtube-dl 简短使用总结

    默认下载bestvideo+bestaudio,并通过ffmpeg -c copy output.mp4 简单的封装进mp4格式,而不进行转码. 有时候bestaudio 是opus编码的,但是mp4 ...

  8. How to Configure YUM to Install Packages From Installation ISO (RHEL)

    1. Mount RHEL Installation ISO mkdir /media/dvd mount /dev/cdrom /media/dvd 2. Get Media ID with the ...

  9. Swift 性能相关

    起初的疑问源自于「在 Swift 中的, Struct:Protocol 比 抽象类 好在哪里?」.但是找来找去都是 Swift 性能相关的东西.整理了点笔记,供大家可以参考一下. 一些疑问 在正题开 ...

  10. Kotlin:数组、字符串模板

    一.数组 Kotlin 中的数组是带有类型参数的类,其元素类型被指定为相应的类型参数,使用 Array 类来表示, Array 类定义了 get 与 set 函数(按照运算符重载约定这会转变为 [ ] ...