170. Rotate List【medium】
Given a list, rotate the list to the right by k
places, where k is non-negative.
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】的更多相关文章
- 2. Add Two Numbers【medium】
2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...
- 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 ...
- 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 ...
- 189. Rotate Array【easy】
189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...
- 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 ...
- 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 ...
- 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 ...
- 75. Find Peak Element 【medium】
75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...
- 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 ...
随机推荐
- 第六章在U盘上运行openwrt(引导)--补
1.前言 前面已经把U盘挂在了703N上了,现在只需要打开路由器,使用TTL串口或者putty(ssh模式需要用户名和密码-第一章刷openwrt的时候已经设置好)登陆路由器. 2.将系统内所有文件同 ...
- React个人学习笔记
元素渲染 通过 ReactDOM.render() 方法渲染页面, 可以使用 ES6 class 来定义一个组件: 如何解析HTMl里面的空格: 1. 使用空格的 unicod 编码 : \u0020 ...
- entity framework 去缓存
MSDN上对MergeOption枚举的定义为: 成员名称 说明 AppendOnly 不会从数据源加载对象上下文中已存在的对象.这是查询或调用 EntityCollection<(Of < ...
- Binary Tree Preorder Traversal -- leetcode
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...
- [转]Configure logging in SSIS packages
本文转自:http://learnsqlwithbru.com/2009/11/26/configure-logging-in-ssis-packages/ n this article we wil ...
- shell学习:几道常见shell习题
1. 编写shell脚本,计算1-100的和: #! /bin/bash sum=0 for i in `seq 1 100`; do sum=$[$i+$sum] done echo $sum 计算 ...
- 如何在Centos7上安装zookeeper 多实例
一.如何在Centos7上安装zookeeper 多实例 cd /usr/local/src/ wget https://mirrors.tuna.tsinghua.edu.cn/apache/zoo ...
- Tomcat集群环境下session共享方案 通过memcached 方法实现
对于web应用集群的技术实现而言,最大的难点就是:如何能在集群中的多个节点之间保持数据的一致性,会话(Session)信息是这些数据中最重要的一块.要实现这一点, 大体上有两种方式:一种是把所有Ses ...
- Android之旅七 Service简介
1. Service是什么:它是一个应用程序组件.没有图形化界面.通常用来处理一些耗时比较长的操作(例如下载.播放MP3等等).可以使用Service更新ContentProvide ...
- linux mysql 更改MySQL数据库目录位置
MySQL默认的数据文件存储目录为/var/lib/mysql.假如要把目录移到/home/data下需要进行下面几步: 1.home目录下建立data目录 cd /home mkdir data 2 ...