给定一个链表,将链表向右旋转 k 个位置,其中 k 是非负数。
示例:
给定 1->2->3->4->5->NULL 且 k = 2,
返回 4->5->1->2->3->NULL.
详见:https://leetcode.com/problems/rotate-list/description/

Java实现:

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) { val = x; }
  7. * }
  8. */
  9. class Solution {
  10. public ListNode rotateRight(ListNode head, int k) {
  11. if(head==null||k<=0){
  12. return head;
  13. }
  14. int len=0;
  15. ListNode cur=head;
  16. while(cur!=null){
  17. ++len;
  18. cur=cur.next;
  19. }
  20. k%=len;
  21. ListNode slow=head;
  22. ListNode fast=head;
  23. for(int i=0;i<k;++i){
  24. if(fast.next!=null){
  25. fast=fast.next;
  26. }else{
  27. return head;
  28. }
  29. }
  30. while(fast.next!=null){
  31. slow=slow.next;
  32. fast=fast.next;
  33. }
  34. fast.next=head;
  35. fast=slow.next;
  36. slow.next=null;
  37. return fast;
  38. }
  39. }

061 Rotate List 旋转链表的更多相关文章

  1. [LeetCode] Rotate List 旋转链表

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

  2. [LeetCode] 61. Rotate List 旋转链表

    Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...

  3. Leetcode61. Rotate List旋转链表

    给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = 2 输出: 4-& ...

  4. 【LeetCode每天一题】Rotate List(旋转链表)

    Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...

  5. [leetcode]61. Rotate List旋转链表

    Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...

  6. leetCode 61.Rotate List (旋转链表) 解题思路和方法

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

  7. [Swift]LeetCode61. 旋转链表 | Rotate List

    Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...

  8. 旋转链表(所有元素往右移) rotate list

    [抄题]: 给定一个链表,旋转链表,使得每个节点向右移动k个位置,其中k是一个非负数 样例 给出链表1->2->3->4->5->null和k=2 返回4->5-& ...

  9. LeetCode 61:旋转链表 Rotate List

    ​给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. Given a linked list, rotate the list to the right by k pla ...

随机推荐

  1. linux命令学习笔记(25):linux文件属性详解

    Linux 文件或目录的属性主要包括:文件或目录的节点.种类.权限模式.链接数量.所归属的用户和用户组. 最近访问或修改的时间等内容.具体情况如下: 命令: ls -lih 输出: [root@loc ...

  2. HihoCoder1333 :平衡树(splay+lazy)(区间加值,区间删除)

    描述 小Ho:好麻烦啊~~~~~ 小Hi:小Ho你在干嘛呢? 小Ho:我在干活啊!前几天老师让我帮忙管理一下团队的人员,但是感觉好难啊. 小Hi:说来听听? 小Ho:事情是这样的.我们有一个运动同好会 ...

  3. P2762 [网络流24题]太空飞行计划问题(最小割)

    地址 最大权闭合子图裸题,不说了吧,求方案就是把s集遍历一遍. 错误记录:dfs那块忘判断残量了,11分×1. #include<cstdio> #include<iostream& ...

  4. ACM学习历程—HDU5265 pog loves szh II(策略 && 贪心 && 排序)

    Description Pog and Szh are playing games.There is a sequence with $n$ numbers, Pog will choose a nu ...

  5. dubbo设计实现的健壮性

    Dubbo 作为远程服务暴露.调用和治理的解决方案,是应用运转的经络,其本身实现健壮性的重要程度是不言而喻的. 这里列出一些 Dubbo 用到的原则和方法. 日志 日志是发现问题.查看问题一个最常用的 ...

  6. C# 获取Console的输入和输出 数据 (异步)

    using System ; using System .Diagnostics; using System .IO;   class Program {     static void Main() ...

  7. XmlSerialize error: There was an error generating the XML document.

    今天遇到一个很火的问题, 一个c#的class 序列化成xml后抛出异常, 信息为: XmlSerialize error: There was an error generating the XML ...

  8. WPF Get Multibinding Expression, Update Source,

    wpf 拿到某个control的multibinding以及其中每个Binding 1. 拿到multibinding      MultiBindingExpression mbe = Bindin ...

  9. SpringMVC之四:渲染Web视图

    理解视图解析 在前面的例子中,我们看到控制器返回的都是一个逻辑视图的名称,然后把这个逻辑视图名称交给view resolver,然后返回渲染后的 html 页面给 client. 将控制器中请求处理的 ...

  10. node.js setup wizard ended prematurely Win7安装nodejs失败解决方法

    笔记本win7在nodejs官方网站下载.msi文件安装,安装到一半的时候,进度条提示:roll back,because of a error.node.JS setup wizard ended ...