Given a linked list, rotate the list to the right by k places, where k is non-negative.

Example 1:

Input: 1->2->3->4->5->NULL, k = 2
Output: 4->5->1->2->3->NULL
Explanation:
rotate 1 steps to the right: 5->1->2->3->4->NULL
rotate 2 steps to the right: 4->5->1->2->3->NULL

Example 2:

Input: 0->1->2->NULL, k = 4
Output: 2->0->1->NULL
Explanation:
rotate 1 steps to the right: 2->0->1->NULL
rotate 2 steps to the right: 1->2->0->NULL
rotate 3 steps to the right: 0->1->2->NULL
rotate 4 steps to the right: 2->0->1->NULL

关键:

1、3个指针,一个指向打断处的后一个结点,并且将要成为新链表头,ListNode* front,用于return;一个指向打断处的前一个节点,并成为链表尾,ListNode* back,用于将尾节点的next置空;一个为当前的尾巴节点,ListNode* link 用于将当前尾节点与头节点连起来。

2、2次O(n)级的遍历,第一次用于求出链表长度,进而求出front和end位置;第二次用于将front和end移动到求出的位置

3、4种特殊情况,一是链表长度为0;二是链表长度为1;三是旋转的步数大于链表长度时要取模;四是旋转步数取模后为0时直接返回。

/**
* 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 NULL;
}else if(head->next==NULL){
return head;
} ListNode* front=head;
ListNode* back=head;
ListNode* link=head; int len=;
while(link->next!=NULL){
len++;
link=link->next;
} k=k%len;
if(k==){
return head;
}
int step=len-k-;
front=front->next; for(int i=;i<step;i++){
back=back->next;
front=front->next;
} back->next=NULL;
link->next=head;
return front;
}
};

leetcode 旋转单链表的更多相关文章

  1. leetCode题解单链表反转

    1.题目描述 反转一个单链表.链表节点结构如下: struct ListNode { int val; ListNode* next; }; 2.问题分析 特殊情况是输入的头结点是一个空的,或者只有一 ...

  2. LeetCode 206 单链表翻转

    https://leetcode.com/problems/reverse-linked-list/ 思路很简单,分别设置三个结点,之后依次调整结点1和结点2的指向关系. Before: pre -& ...

  3. leetcode 去除单链表倒数第k个节点

    Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...

  4. [LeetCode] Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  5. LeetCode 笔记系列六 Reverse Nodes in k-Group [学习如何逆转一个单链表]

    题目:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...

  6. LeetCode 单链表专题 (一)

    目录 LeetCode 单链表专题 <c++> \([2]\) Add Two Numbers \([92]\) Reverse Linked List II \([86]\) Parti ...

  7. [LeetCode] 142. Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...

  8. [LeetCode 206] Reverse Linked List 翻转单链表

    本题要求将给定的单链表翻转,是校招面试手撕代码环节的高频题,能很好地考察对单链表这一最简单数据结构的理解:可以使用迭代和递归两种方法对一个给定的单链表进行翻转,具体实现如下: class Soluti ...

  9. [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环

    题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...

随机推荐

  1. sql row_number 用法

    自己研究了一下RowNum ,发现这样的分页挺清晰的 --第几页,一页多少行declare @PageIndex int,@PageMax intset @PageIndex =1set @PageM ...

  2. 用javascript技术读取注册表中软件安装位置并启动本地软件

    1.首先读取注册表中本地软件安装的位置,如果未安装则无就跳转到下载页面. 2.启动软件,关闭页面. 3.如报错提示. <SCRIPT language=javascript>  <! ...

  3. Centos7 之安装Logstash ELK stack 日志管理系统

    一.介绍 The Elastic Stack - 它不是一个软件,而是Elasticsearch,Logstash,Kibana 开源软件的集合,对外是作为一个日志管理系统的开源方案.它可以从任何来源 ...

  4. ThinkPHP项目笔记之控制器常用语法

    如,有数据表:tmp,以下以此为例. $a = M('Tmp'); $a -> select(); $a -> where(condition)->select(); $a -> ...

  5. JSP 页面中用绝对路径显示图片

    首先,图片和工程不在一个盘符下.图片也不能放到工程下.  在JSP 文件中 <img src="E:/图片/1.jpg"/>  这样是引不到图片的.因为,JSP页面在引 ...

  6. Asp.Net MVC 把PartialView、View转换成字符串

    在开发中有时要在后台获得某个View 或者 PartialView 生成的字符串,只要你熟悉Asp.Net MVC  生命周期就能理解和敲出下面的代码.没什么高深的,直接上代码: 1,输出View H ...

  7. MySQL5.6绿色版安装(mysql-5.6.24-winx64.zip)

    1.数据库安装 Mysql官方网站:http://www.mysql.com/,数据库下载地址:http://www.mysql.com/downloads/.从官方网站可以找到两种文件包,一种是ex ...

  8. Struts2_day02--Action获取表单提交数据

    Action获取表单提交数据 1 之前web阶段,提交表单到servlet里面,在servlet里面使用request对象里面的方法获取,getParameter,getParameterMap 2 ...

  9. 编程之美 set 4 找到符合条件的数

    题目 任意给定一个正整数 N, 求一个最小的正整数 M (M > 1), 使得 N*M 的十进制表达式中只有 0 和 1. 解法 1. 枚举0,1能够组成的数字, 可以组成一颗二叉树 然后由 B ...

  10. 阿里云CentOS6.8安装MySQL5.6

    1.使用SSH Secure Shell工具连接阿里云服务器 2.使用SSH Secure File Transfer工具上传MySQL压缩包 3.解压MySQL压缩包到指定目录(需要在先/usr/l ...