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. 网络工程师岗位基础面试题【适用于CCNA/CCNP基础】

    网络工程师岗位基础面试题[适用于CCNA/CCNP基础] 1: 交换机是如何转发数据包的?交换机通过学习数据帧中的源MAC地址生成交换机的MAC地址表,交换机查看数据帧的目标MAC地址,根据MAC地址 ...

  2. 过滤器chain.doFilter(request,response)的含义

    过滤器的生命周期一般都要经过下面三个阶段: 初始化: 当容器第一次加载该过滤器时,init() 方法将被调用.该类在这个方法中包含了一个指向 Filter Config 对象的引用.我们的过滤器实际上 ...

  3. Intellij IDEA中使用log4j日志

    一.在pom.xml中添加依赖 <dependency> <groupId>log4j</groupId> <artifactId>log4j</ ...

  4. Hadoop Maven pom文件示例

    Hadoop Maven pom文件示例 @(Hadoop) <?xml version="1.0" encoding="UTF-8"?> < ...

  5. 云计算之路-阿里云上-新发现:又一种与虚拟内存有关的CPU波动情况

    在云上真是无奇不有,昨天偶然间发现在IIS的应用程序池回收设置中,仅仅设置了一下基于虚拟内存限制的回收,就引发了CPU有规律的波动.在这篇博文中,我们将向大家汇报一下云计算之路上的这个小发现. 在之前 ...

  6. 轻量级的前端UI开发框架 - UIkit

    来源:GBin1.com UIkit是YOOtheme团队开发的开源的前端UI界面框架,可以帮助你快速的开发和创建前端UI界面. 基于下列开源项目: LESS jQuery normalize.css ...

  7. JMeter 一:Elements of a Test Plan

    参考:http://jmeter.apache.org/usermanual/test_plan.html 最小测试集包括:Test Plan,一个Thread Group,以及一个或多个Sample ...

  8. std::shared_ptr 和 std::weak_ptr的用法以及引用计数的循环引用问题

    在std::shared_ptr被引入之前,C++标准库中实现的用于管理资源的智能指针只有std::auto_ptr一个而已.std::auto_ptr的作用非常有限,因为它存在被管理资源的所有权转移 ...

  9. Cacti监控MySQL实现过程中碰到的问题解汇总

    前言:cacti监控mysql服务器的大概50张graphs都弄出来了,也出图了,当中遇到一些问题,印象比較深刻的记录例如以下: (一):加入io监控 点击Create Graphs for this ...

  10. 【jquery的setTimeOut定时器使用】

    目的:用户提交表单,一直触发校验事件. 1.效果: 2.代码: <!-- 去掉必填提示 --> <script type="text/javascript"> ...