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. TIM—基本定时器

    本章参考资料:< STM32F4xx 参考手册>.< STM32F4xx 规格书>.库帮助文档< stm32f4xx_dsp_stdperiph_lib_um.chm&g ...

  2. word字号

    1 大特号 63 2 特 号 54 3 初 号 42 4 小初号 36 5 大一号 31.5 6 一 号 28 7 小一号 24 8 二 号 21 9 小二号 18 10 三 号 16 11 小三号  ...

  3. java POi excel 写入大批量数据

    直接贴代码: package jp.co.misumi.mdm.batch.common.jobrunner; import java.io.File; import java.io.FileNotF ...

  4. Cookie js 操作

    从事web开发也有些日子了,cookie 是个啥差不多能说明白,可是实际自己一上手操作就是得去搜索(你们懂的),结果被鄙视了...所以就写一篇博文做为自己的学习笔记,嘿嘿,博客的好处在此体现出来了. ...

  5. 监听过多,会抛tooManyListener例外

    在生成一个窗体的时候,点击窗体的右上角关闭按钮激发窗体事件的方法:窗体Frame为事件源,WindowsListener接口调用Windowsclosing(). 为了配合后面的实现,我们必须将Win ...

  6. xsocket:空闲超时问题。

    XSocket是什么? java的nio的封装. 详情: 1. http://xsocket.sourceforge.net/core/apidocs/2_1/index.html 2. http:/ ...

  7. redis的使用和安装,redis基础和高级部分

    redis的使用和安装,redis基础和高级部分 在后端开发中,为了提高性能,对于一些经常查询但是又不太变化的内容会使用redis,比如前端的列表展示项等,如果数据有变化也可以清空缓存,让前端查一次数 ...

  8. ThinkPHP项目笔记之模板篇

    顾名思义:模板就是网页页面,有的是静态,有的的动态 基本语法: 1. <li><a href="{:U('User/searchlist')}">返回列表& ...

  9. Mac终端使用mysql

    1.安装完mysql后给mysql命别名 alias mysql=/usr/local/mysql/bin/mysql aliasmysqladmin=/usr/local/mysql/bin/mys ...

  10. Pecan

    什么是peacn Pecan是一个轻量级的基于Python的Web框架, Pecan的目标并不是要成为一个“full stack”的框架, 因此Pecan本身不支持类似Session和Database ...