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 ...
随机推荐
- 【mysql】mysql中varcher属性最大值能存多长
1.首先理解varchar(n),n表示什么 MySQL5.0.3之前varchar(n)这里的n表示字节数 MySQL5.0.3之后varchar(n)这里的n表示字符数,比如varchar(200 ...
- python笔记4-遍历文件夹目录os.walk()
前言 如何遍历查找出某个文件夹内所有的子文件呢?并且找出某个后缀的所有文件 walk功能简介 1.os.walk() 方法用于通过在目录树种游走输出在目录中的文件名,向上或者向下. 2.walk()方 ...
- Android 卡顿优化 2 渲染优化
1.概述 2015年初google发布了Android性能优化典范,发了16个小视频供大家欣赏,当时我也将其下载,通过微信公众号给大家推送了百度云的下载地址(地址在文末,ps:欢迎大家订阅公众号),那 ...
- 靠谱助手 http://www.kpzs.com/
靠谱助手 http://www.kpzs.com/ 靠谱助手是于2013年5月18日推出的一个专为第三方智能安卓引擎提供管理的免费应用程序,是国内最完美的PC端管理软件. 安卓引擎支持微信.陌陌等日常 ...
- 用LaTeX写线性规划
线性规划由目标函数和若干约束构成,Latex中并没有直接的命令来写线性规划.简单的做法是使用\begin{eqnarray} … \end{eqnarray}命令,但eqnarray命令是使若干方程按 ...
- 十一.spring-boot 添加http自动转向https
SSL是为网络通信提供安全以及保证数据完整性的的一种安全协议,SSL在网络传输层对网络连接进行加密. 例:cas 的单点登陆就用到了SSL 一.安全证书的生成 1.可以使用jdk自带的证书生成工具,j ...
- C语言-对一个结构体中的字段进行排序
这是帮别人做的一个题目,好久没有接触过C语言了.有点发怵,只是似乎找回点当时学C语言,做课程设计的感觉. 题目:定义一个数组(学生结构体数组),里面包括学号.姓名.身份证和三科学生成绩.要求写一个函数 ...
- 两款工控控件对比评测:Iocomp和ProEssentials
对于程序员来说,要凭一己之力开发出漂亮逼真的工控仪表和工控图表是非常耗时间和精力的,那么使用专业的第三方控件就是不错的选择,不仅节约开发时间,降低了项目风险,最重要的是第三方控件写的程序更专业,工控图 ...
- 公司机器(线上机器)启动ftp任务的命令
这个命令: /usr/local/proftp/sbin/proftpd 注意要在root账户,并且kill掉原来的同名进程.
- C#动态调用webservice方法
摘 自: http://www.hao5191.cn/a/chengxukaifa/c_/20130109/115819.html using System; using System.Collect ...