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 ...
随机推荐
- 【SQL Sever】安装过程
下载了sql sever,如下: 首先把iso解压,如下: 1.点击 2.打开页面后 3. 接下来直接下一步下一步 完成之后,需要重启计算机才能使用! 4. 重启之后,进入配置工具 将所有的端口号更改 ...
- 关于TagHelper的那些事情——如何自定义TagHelper(TagHelper基类)
写在开头 前面介绍了TagHelper的基本概念和内嵌的TagHelpers,想必大家对TagHelper都有一定的了解.TagHelper看上去有点像WebControl,但它不同于WebContr ...
- 玩转Google开源C++单元测试框架Google Test系列(gtest)(转)
转自:http://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html 前段时间学习和了解了下Google的开源C++单元测试框架Googl ...
- lumisoft会将eml后缀格式的附件给解析成文本,这里保存成文件
MIME_Entity[] attachments = mime.Attachments; foreach (MIME_Entity entity in attachments) { string f ...
- java中return的作用
脑补一下基础的东西,return的用法. return的常用作用有以下两种第一种用法是返回参数所用的关键字,假如一个有返回值的方法执行完了之后需要返回一个参数,示例: public string fu ...
- Java笔记13:统计文件中每个字符出现的次数
一.代码实现 import java.io.*; import java.util.*; /** 功能:统计文件中每个字符出现的次数 思路: 1.定义字符读取(缓冲)流 2.循环读取文件里的字符,用一 ...
- C#/Sqlite-SQLite PetaPoco django 打造桌面程序
为什么是 SQLite? 在以前的程序中, 我通常会使用 MySQL. 如果使用你程序的用户是一个软件小白, 而且远在另一个城市, 那么让她安装和部署 MySQL 将是一场噩梦: 她需要配置服务, 面 ...
- linux 的计划任务 cron
https://serverfault.com/questions/587696/how-to-restart-php-fpm-from-cron 我也遇到了这个问题,想用cron 来启动php-fp ...
- 省市区县三级联动JAVA+MySQL+JQuery
场景介绍: 由于项目要求,需要做一个进件(新增)功能,而该功能里要用到车品牌.车系.车型的联动查询,也就是经典的三级联动. 大体思路如下: 进入页面,会把所有的车品牌(第一级)在后台查询出来,放到缓存 ...
- Remove all your local git branches but keep master
Sometimes after a sprint, all the remaining branches are just taking up space. Here's a small snippe ...