Given a list, rotate the list to the right by k places, where k is non-negative.

For example:
Given 1->2->3->4->5->NULL and k = 2,
return 4->5->1->2->3->NULL.

这个题目的大意是要把链表的尾部的k节点旋转到头部,但是k可能大于链表的长度。此时要旋转的长度就是k%list.length。

解决这个问题我分为了两个步骤:

  1. 求链表的长度;
  2. 进行旋转操作;
 public class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(k<0 || head==null) return null; int length = 0;
ListNode temp = head;
ListNode temp2 = head;
while(temp!=null){
temp = temp.next;
length++;
}
k = k%length;
temp = head; while(temp.next != null){
if(k<1) temp2 = temp2.next;
temp = temp.next;
k--;
}
temp.next = head;
head = temp2.next;
temp2.next = null;
return head;
}
}

参考了别人的代码,找到了更加简便的方法,这个方法只用到了一个指针,很值得我们学习。

 public ListNode rotateRight(ListNode head, int k) {
if(head == null || k == 0) {
return head;
}
ListNode p = head;
int len = 1;
while(p.next != null) {
p = p.next;
len++;
}
p.next = head;
k %= len;
for(int i = 0; i < len - k; i++) {
p = p.next;
}
head = p.next;
p.next = null;
return head;
}

LeetCode OJ 61. Rotate List的更多相关文章

  1. 【LeetCode】61. Rotate List 解题报告(Python)

    [LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  2. 【一天一道LeetCode】#61. Rotate List

    一天一道LeetCode系列 (一)题目 Given a list, rotate the list to the right by k places, where k is non-negative ...

  3. LeetCode OJ 48. Rotate Image

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  4. LeetCode OJ 189. Rotate Array

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  5. 【leetcode】61. Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  6. LeetCode OJ:Rotate List(旋转链表)

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  7. LeetCode OJ:Rotate Image(旋转图片)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  8. LeetCode OJ:Rotate Array(倒置数组)

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  9. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

随机推荐

  1. JSONObject处理java.util.Date

    JSONObject的内容为: {"userId":"A000004FFDCE14","userName":"好好干g" ...

  2. mac下配置gdb调试golang

    mac下配置gdb调试golang 原文链接 https://sourceware.org/gdb/wiki/BuildingOnDarwin Building GDB for Darwin Crea ...

  3. 【.NET】上传文件,生成缩略图

    类名:Upload using System; using System.Collections; using System.ComponentModel; using System.Data; us ...

  4. UVALive 6910 Cutting Tree(并查集应用)

    总体来说,这个题给的时间比较长,样例也是比较弱的,别的方法也能做出来. 我第一次使用的是不合并路径的并查集,几乎是一种暴力,花了600多MS,感觉还是不太好的,发现AC的人很多都在300MS之内的过得 ...

  5. wordpress 删除底部"自豪地采用 WordPress"

    找到footer.php文件,删除以下代码即可: <footer id="colophon" class="site-footer" role=" ...

  6. iOS开发tableview二级联动的细节实现中注意的细节总结

    首先说网络慢带来的数据显示问题 可以通过判断请求参数是否一致来刷新tableview. SJBCategaryModel * categaryModel = self.categarys[Catega ...

  7. UITableView控制头部View

    首先我们先集成一个UIView做一个自己的View #import <UIKit/UIKit.h> @interface MyView : UIView @property (nonato ...

  8. C# Monads的实现(一)

    了解Haskell语言的朋友都知道它是一门纯函数式编程,输出结果只跟输入参数相关,这导致Haskell不能有输入输出函数,因为不同的环境下,输入相同,但输出可能有所不同.Haskell语言中,变量的值 ...

  9. Rancher安装使用

    官网 http://docs.rancher.com/rancher/latest/en/quick-start-guide/#add-hosts 安装步骤: 1 Start up a Linux m ...

  10. 读取memo中某行内容

    方法1 可用以下代码读取Memo中指定行的内容: var   aLine:String; begin   aLine:=Memo1.Lines[2]; end; 在使用中,读取的行在Memo中需要保证 ...