题目:

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.

链接:http://leetcode.com/problems/rotate-list/

题解:

先遍历一次链表,将尾部和头部相连,再进行移动。注意右移k步相当于prehead travel len - k步。

Time Complexity - O(n), Space Complexity - O(1)

public class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(head == null || k == 0)
return head;
ListNode node = head;
int len = 1;
while(node.next != null){
node = node.next;
len ++;
}
node.next = head;
k %= len;
for(int i = 0; i < len - k; i++)
node = node.next; head = node.next;
node.next = null;
return head;
}
}

二刷:

主要思路还是把链表结成环。

  1. 先求出链表长度。
  2. 将链表结成环
  3. 计算k的合理范围。向右移动k位,就相当于从表头向左travel len - k个单位: 
    1. 假如k < 0,那么说明向左移动,我们只需要计算-k % len就可以了
    2. 假如k > 0,我们需要把k变换为 len - k % len
  4. 接下来当k > 0的时候,我们从node继续往下travel, k--
  5. 第4步的遍历结束时,我们可以设置新的head = node.next,然后在此处断开环, node.next = null
  6. 最后返回head。

Java:

Time Complexity - O(n), Space Complexity - O(1)

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode rotateRight(ListNode head, int k) {
if (head == null || head.next == null) {
return head;
}
ListNode node = head;
int len = 1;
while (node.next != null) {
node = node.next;
len++;
}
node.next = head;
if (k < 0) {
k = -k % len;
} else {
k = len - k % len;
}
while (k > 0) {
node = node.next;
k--;
}
head = node.next;
node.next = null;
return head;
}
}

三刷:

看到k是non-negative的我就放心了。这里要注意我们结成环以后,在链表尾部寻找新的表头,需要移动的距离是  len - k % len。这样node.next就是新的表头。

Java:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode rotateRight(ListNode head, int k) {
if (head == null || k <= 0) return head;
ListNode node = head;
int len = 1;
while (node.next != null) {
node = node.next;
len++;
}
node.next = head; k = len - k % len;
while (k > 0) {
node = node.next;
k--;
}
head = node.next;
node.next = null;
return head;
}
}

61. Rotate List的更多相关文章

  1. 61. Rotate List(M);19. Remove Nth Node From End of List(M)

    61. Rotate List(M) Given a list, rotate the list to the right by k places, where k is non-negative. ...

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

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

  3. Leetcode#61 Rotate List

    原题地址 我一直不太理解为什么叫rotate,翻译成"旋转"吧,似乎也不像啊.比如: 1->2->3->4->5->NULL 向右旋转2的距离,变成了 ...

  4. [LeetCode] 61. Rotate List 解题思路

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

  5. LeetCode OJ 61. Rotate List

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

  6. 【leetcode】61. 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. [leetcode]61. Rotate List旋转链表

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

  9. 61. Rotate List(List)

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

随机推荐

  1. ios特性访问器方法(setter和getter)

    Employee.h @interface Employee:NSObject { int _employeeNumber; NSString *_name; Employee*_supervisit ...

  2. windows创建桌面快捷方式的VBA脚本

    Dim wShell, oShortcut    'Dim strDesktop$ ' 为了与VBS兼容,    Dim strDesktop    ' 这里改写一下,测试通过...    Set w ...

  3. System V消息队列

    消息的基本属性 System V的消息属性包含在一个msqid_ds的结构中 struct msqid_ds{ struct ipc_cerm msg_perm; //读取权限, 0644, 0777 ...

  4. JDBC 基本操作: CRUD

    Jdbc中的statement对象用于向数据库发送SQL语句,想完成对数据库的增删改查,只需要通过这个对象向数据库发送增删改查语句即可. Statement对象的executeUpdate方法,用于向 ...

  5. WCF-Configuration

    Host-Configuration <?xml version="1.0"?> <configuration> <configSections> ...

  6. Oracle物理的体系结构

    体系结构图的学习: 老余服装店的故事 结构图: SQL查询语句 SGA 共享池shared pool 数据缓存区Buffer cache PGA 进程 SQL更新语句 SGA: 日志缓存区 日志文件 ...

  7. CentOS服务器Http压力测试之ab

    ab的全称是Apache Bench,是Apache自带的网络压力测试工具,相比于LR.JMeter,是我所知道的 Http 压力测试工具中最简单.最通用的. ab命令对发出负载的计算机要求很低,不会 ...

  8. c++各种排序

    1.插入排序 void InsertSort(int a[], int n) { int temp, i, j; ; i < n; i++) { ]) { temp = a[i]; ; j &g ...

  9. cocos2dx中的菜单项CCMenuItem及其五个子类的使用

    /*CCMenuItem是一个虚基类,因此必须实现它的五个子类之一,再把子类对象赋给父类指针,相当于多态*/ CCMenuItem *fontItem = CCMenuItemFont::create ...

  10. html5音频、视频

    1.插入一个视频:<video src="test.webm" width="800" height="600"></vi ...