题目链接

  题目要求:

  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 and k = 2,
  return 4->5->1->2->3->NULL.

  比较直观的一个解法就是先求出链表长度,然后再确定从头节点位移到分割节点处的步数。接下来就是分割链表和拼接了。具体程序(8ms)如下:

 /**
* 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 || !head->next || k == )
return head; int len = ;
ListNode *start = head;
while(start)
{
len++;
start = start->next;
}
k = len - k % len;
if(k == || k == len)
return head; ListNode *dummy = new(nothrow) ListNode(INT_MIN);
assert(dummy); start = head;
for(int i = ; i < k - ; i++)
start = start->next; dummy->next = start->next;
start->next = nullptr; start = dummy;
while(start->next)
start = start->next;
start->next = head; head = dummy->next;
delete dummy;
dummy = nullptr; return head;
}
};

  这题还有另一个很巧妙的解法:首先从head开始跑,直到最后一个节点,这时可以得出链表长度len。然后将尾指针指向头指针,将整个圈连起来,接着往前跑len – k%len,从这里断开,就是要求的结果了。

  具体程序(12ms)如下:

 /**
* 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 || !head->next || k == )
return head; int len = ;
ListNode *start = head;
while(start->next)
{
len++;
start = start->next;
}
k = len - k % len;
if(k == || k == len)
return head; start->next = head;
for(int i = ; i < k; i++)
start = start->next; head = start->next;
start->next = nullptr; return head;
}
};

LeetCode之“链表”:Rotate List的更多相关文章

  1. LeetCode 单链表专题 (一)

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

  2. 【LeetCode】61. Rotate List 解题报告(Python)

    [LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  3. Leetcode解题-链表(2.2.0)基础类

    1 基类的作用 在开始练习LeetCode链表部分的习题之前,首先创建好一个Solution基类,其作用就是: Ø  规定好每个子Solution都要实现纯虚函数test做测试: Ø  提供了List ...

  4. 【算法题 14 LeetCode 147 链表的插入排序】

    算法题 14 LeetCode 147 链表的插入排序: 解题代码: # Definition for singly-linked list. # class ListNode(object): # ...

  5. LeetCode 61:旋转链表 Rotate List

    ​给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. Given a linked list, rotate the list to the right by k pla ...

  6. LeetCode OJ:Rotate List(旋转链表)

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  7. 【一天一道LeetCode】#61. Rotate List

    一天一道LeetCode系列 (一)题目 Given a list, rotate the list to the right by k places, where k is non-negative ...

  8. [Swift]LeetCode61. 旋转链表 | Rotate List

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

  9. LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors

    1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...

随机推荐

  1. RxJava(十一)defer操作符实现代码支持链式调用

    欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/52597643 本文出自:[余志强的博客] 一.前言 现在越来越多An ...

  2. Swift:Foundation框架中的NS前缀的由来

    可能大家对于著名的NS前缀的由来有一些疑问. 绝大多数这些NS前缀的类是NeXTSTEP操作系统中Foundation框架里的一部分,而该操作系统是OS X的基础. NeXTSTEP的程序员对它们的类 ...

  3. 常用的DDL语句

    create database mydb1; 创建一个名称为mydb1的数据库. use db_name; 切换数据库 ; show databases; 查看所有的数据库: select datab ...

  4. ant编译mysql驱动

    修改驱动源码后需要重新编译构建,由于mysql编译需要两个jdk版本且还需要hibernate4和junit,这里记录下. 安装ant. 配置两个jdk,5和8.并修改build.xml配置,如下: ...

  5. 熟悉Python的各种基础小算法

    网上有一个Python100小例子的栏目,里面代码良莠不齐,于是下面就自己实现了其中的一些案例. 01.py # coding:utf-8 import sys reload(sys) sys.set ...

  6. UISearchController替换UISearchDisplayController

    随着iOS 的升级,iOS 7的占有率更低了.Xcode 升级到Xcode 8之后,对iOS 应用支持的最低版本,iOS 7也被抛弃了.我在新项目中也是最低支持到iOS 8,因此工程里也是各种警告.首 ...

  7. 【SSH系列】---Hibernate的基本映射

    开篇前言       在前面的博文中,小编分别介绍了[SSH系列]-- hibernate基本原理&&入门demo,通过这篇博文,小伙伴们对hibernate已经有了基本的了解,以及h ...

  8. x264源代码简单分析:宏块编码(Encode)部分

    ===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...

  9. iOS中监控软键盘显示或隐藏的可靠方法

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 如果你试图在软键盘的显示或隐藏时去改变的UI界面结构,仅有的方 ...

  10. Android缩放动画

    Android缩放动画 核心方法 public void startAnimation(Animation animation) 执行动画,参数可以是各种动画的对象,Animation的多态,也可以是 ...