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.

解法一:

  1. public class Solution {
  2. private int getLength(ListNode head) {
  3. int length = 0;
  4. while (head != null) {
  5. length ++;
  6. head = head.next;
  7. }
  8. return length;
  9. }
  10.  
  11. public ListNode rotateRight(ListNode head, int n) {
  12. if (head == null) {
  13. return null;
  14. }
  15.  
  16. int length = getLength(head);
  17. n = n % length;
  18.  
  19. ListNode dummy = new ListNode(0);
  20. dummy.next = head;
  21. head = dummy;
  22.  
  23. ListNode tail = dummy;
  24. for (int i = 0; i < n; i++) {
  25. head = head.next;
  26. }
  27.  
  28. while (head.next != null) {
  29. tail = tail.next;
  30. head = head.next;
  31. }
  32.  
  33. head.next = dummy.next;
  34. dummy.next = tail.next;
  35. tail.next = null;
  36. return dummy.next;
  37. }
  38. }

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

解法二:

  1. class Solution {
  2. public:
  3. /**
  4. * @param head: the list
  5. * @param k: rotate to the right k places
  6. * @return: the list after rotation
  7. */
  8. ListNode *rotateRight(ListNode *head, int k) {
  9. if (head == NULL) {
  10. return head;
  11. }
  12.  
  13. int len = ;
  14. for (ListNode *node = head; node != NULL; node = node->next) {
  15. len++;
  16. }
  17. k = k % len;
  18.  
  19. if (k == ) {
  20. return head;
  21. }
  22.  
  23. ListNode *fast = head;
  24. for (int i = ; i < k; i++) {
  25. fast = fast->next;
  26. }
  27.  
  28. ListNode *slow = head;
  29. while (fast->next != NULL) {
  30. slow = slow->next;
  31. fast = fast->next;
  32. }
  33.  
  34. fast->next = head;
  35. head = slow->next;
  36. slow->next = NULL;
  37.  
  38. return head;
  39. }
  40. };

不带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. 【mysql】mysql中varcher属性最大值能存多长

    1.首先理解varchar(n),n表示什么 MySQL5.0.3之前varchar(n)这里的n表示字节数 MySQL5.0.3之后varchar(n)这里的n表示字符数,比如varchar(200 ...

  2. python笔记4-遍历文件夹目录os.walk()

    前言 如何遍历查找出某个文件夹内所有的子文件呢?并且找出某个后缀的所有文件 walk功能简介 1.os.walk() 方法用于通过在目录树种游走输出在目录中的文件名,向上或者向下. 2.walk()方 ...

  3. Android 卡顿优化 2 渲染优化

    1.概述 2015年初google发布了Android性能优化典范,发了16个小视频供大家欣赏,当时我也将其下载,通过微信公众号给大家推送了百度云的下载地址(地址在文末,ps:欢迎大家订阅公众号),那 ...

  4. 靠谱助手 http://www.kpzs.com/

    靠谱助手 http://www.kpzs.com/ 靠谱助手是于2013年5月18日推出的一个专为第三方智能安卓引擎提供管理的免费应用程序,是国内最完美的PC端管理软件. 安卓引擎支持微信.陌陌等日常 ...

  5. 用LaTeX写线性规划

    线性规划由目标函数和若干约束构成,Latex中并没有直接的命令来写线性规划.简单的做法是使用\begin{eqnarray} … \end{eqnarray}命令,但eqnarray命令是使若干方程按 ...

  6. 十一.spring-boot 添加http自动转向https

    SSL是为网络通信提供安全以及保证数据完整性的的一种安全协议,SSL在网络传输层对网络连接进行加密. 例:cas 的单点登陆就用到了SSL 一.安全证书的生成 1.可以使用jdk自带的证书生成工具,j ...

  7. C语言-对一个结构体中的字段进行排序

    这是帮别人做的一个题目,好久没有接触过C语言了.有点发怵,只是似乎找回点当时学C语言,做课程设计的感觉. 题目:定义一个数组(学生结构体数组),里面包括学号.姓名.身份证和三科学生成绩.要求写一个函数 ...

  8. 两款工控控件对比评测:Iocomp和ProEssentials

    对于程序员来说,要凭一己之力开发出漂亮逼真的工控仪表和工控图表是非常耗时间和精力的,那么使用专业的第三方控件就是不错的选择,不仅节约开发时间,降低了项目风险,最重要的是第三方控件写的程序更专业,工控图 ...

  9. 公司机器(线上机器)启动ftp任务的命令

    这个命令: /usr/local/proftp/sbin/proftpd 注意要在root账户,并且kill掉原来的同名进程.

  10. C#动态调用webservice方法

    摘 自: http://www.hao5191.cn/a/chengxukaifa/c_/20130109/115819.html using System; using System.Collect ...