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

 
Example

Given 1->2->3->4->5 and k = 2, return 4->5->1->2->3.

解法一:

 public class Solution {
private int getLength(ListNode head) {
int length = 0;
while (head != null) {
length ++;
head = head.next;
}
return length;
} public ListNode rotateRight(ListNode head, int n) {
if (head == null) {
return null;
} int length = getLength(head);
n = n % length; ListNode dummy = new ListNode(0);
dummy.next = head;
head = dummy; ListNode tail = dummy;
for (int i = 0; i < n; i++) {
head = head.next;
} while (head.next != null) {
tail = tail.next;
head = head.next;
} head.next = dummy.next;
dummy.next = tail.next;
tail.next = null;
return dummy.next;
}
}

快慢指针  + dummy节点,参考@NineChapter 的代码

解法二:

 class Solution {
public:
/**
* @param head: the list
* @param k: rotate to the right k places
* @return: the list after rotation
*/
ListNode *rotateRight(ListNode *head, int k) {
if (head == NULL) {
return head;
} int len = ;
for (ListNode *node = head; node != NULL; node = node->next) {
len++;
}
k = k % len; if (k == ) {
return head;
} ListNode *fast = head;
for (int i = ; i < k; i++) {
fast = fast->next;
} ListNode *slow = head;
while (fast->next != NULL) {
slow = slow->next;
fast = fast->next;
} fast->next = head;
head = slow->next;
slow->next = NULL; return head;
}
};

不带dummy节点的解法,参考@NineChapter 的代码

170. Rotate List【medium】的更多相关文章

  1. 2. Add Two Numbers【medium】

    2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...

  2. 92. Reverse Linked List II【Medium】

    92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...

  3. 82. Remove Duplicates from Sorted List II【Medium】

    82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...

  4. 189. Rotate Array【easy】

    189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...

  5. 61. Search for a Range【medium】

    61. Search for a Range[medium] Given a sorted array of n integers, find the starting and ending posi ...

  6. 62. Search in Rotated Sorted Array【medium】

    62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...

  7. 74. First Bad Version 【medium】

    74. First Bad Version [medium] The code base version is an integer start from 1 to n. One day, someo ...

  8. 75. Find Peak Element 【medium】

    75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...

  9. 159. Find Minimum in Rotated Sorted Array 【medium】

    159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...

随机推荐

  1. Signavio

    Signavio web建模器 从version 4.1开始,jBPM绑定了一个完全开源的基于web的BPM设计器工具 叫做'Signavio'.这个Signavio web建模器是和JBoss jB ...

  2. JAVA常见算法题(二十九)

    package com.forezp.util; import java.util.Scanner; /** * 判断输入的5个字符串的最大长度,并输出 * * * @author Administr ...

  3. BrowserSync-多浏览器测试工具

    阅读目录 自动刷新 介绍BrowserSync BrowserSync具体使用 BrowserSync配合gulp 自动刷新 自动刷新,顾名思义,就是不用我们去F5刷新.假设有一天我们写代码,只需要c ...

  4. What is required for a successful backup of all files during hoi backup?

    There is a typo in the body of this question. It should be "Hot" instead of "hoi" ...

  5. springmvc实现简单的拦截器

    SpringMVC 中的Interceptor 拦截请求是通过HandlerInterceptor 来实现的.在SpringMVC 中定义一个Interceptor 非常简单,主要有两种方式,第一种方 ...

  6. java--生成实体类方法

    由于工作中使用eclipse开发,没有安装hibernate插件,所以无法使用自动生成实体类的功能,为了方便在网上找了段别人写的自动生成实体类方法,稍加修改,直接运行就可以生成对应表的实体类. 注意使 ...

  7. 【笔记】js原生方法 在元素外部或内部实现添加元素功能(类似jq 的 insert 和 append)

    介绍的这个方法是:insetAdjacentHTML() 方法 此方法接收两个参数: 第一个参数必为下列值: beforebegin:在调用的元素外部的前面添加一个目标元素 afterend:在调用元 ...

  8. (转)IntelliJ IDEA下的使用git

    1.git简介 Git是目前流行的分布式版本管理系统.它拥有两套版本库,本地库和远程库,在不进行合并和删除之类的操作时这两套版本库互不影响.也因此其近乎所有的操作都是本地执行,所以在断网的情况下任然可 ...

  9. ini配置文件的读取

    .ini 文件是Initialization File的缩写,即初始化文件.是windows的系统配置文件所采用的存储格式,统管windows的各项配置,一般用户就用windows提供的各项图形化管理 ...

  10. Linux系统目录结构,Shell脚本;关闭和开启防火墙

    Linux系统目录结构 目录 描述 备注 /bin a.存放着最经常使用的命令 b.可执行文件,用户命令 c.构建最小系统所需要的命令 /boot a.内核与启动文件 b.系统启动相关文件 c.启动L ...