Rotate List

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.

  1. /*************************************************************************
  2. > File Name: LeetCode061.c
  3. > Author: Juntaran
  4. > Mail: JuntaranMail@gmail.com
  5. > Created Time: Tue 17 May 2016 18:47:57 PM CST
  6. ************************************************************************/
  7.  
  8. /*************************************************************************
  9.  
  10. Rotate List
  11.  
  12. Given a list, rotate the list to the right by k places,
  13. where k is non-negative.
  14.  
  15. For example:
  16. Given 1->2->3->4->5->NULL and k = 2,
  17. return 4->5->1->2->3->NULL.
  18.  
  19. ************************************************************************/
  20.  
  21. #include <stdio.h>
  22. /**
  23. * Definition for singly-linked list.
  24. * struct ListNode {
  25. * int val;
  26. * struct ListNode *next;
  27. * };
  28. */
  29. struct ListNode* rotateRight(struct ListNode* head, int k)
  30. {
  31. struct ListNode* fast = head;
  32. struct ListNode* slow = head;
  33. struct ListNode* newhead = head;
  34. int length = ;
  35.  
  36. if( head == NULL || k < )
  37. {
  38. return head;
  39. }
  40.  
  41. while( fast->next != NULL )
  42. {
  43. fast = fast->next;
  44. length ++;
  45. }
  46. fast = head;
  47. k = k % length;
  48. // printf("%d %d\n",k,length);
  49.  
  50. while( k > )
  51. {
  52. fast = fast->next;
  53. if( fast == NULL )
  54. {
  55. return head;
  56. }
  57. k --;
  58. }
  59.  
  60. while( fast->next != NULL )
  61. {
  62. slow = slow->next;
  63. fast = fast->next;
  64. }
  65. fast->next = head;
  66. newhead = slow->next;
  67. slow->next = NULL;
  68.  
  69. return newhead;
  70. }

LeetCode 61的更多相关文章

  1. LeetCode: 61. Rotate List(Medium)

    1. 原题链接 https://leetcode.com/problems/rotate-list/description/ 2. 题目要求 给出一个链表的第一个结点head和正整数k,然后将从右侧开 ...

  2. LeetCode 61:旋转链表 Rotate List

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

  3. [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 ...

  4. Java实现 LeetCode 61 旋转链表

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

  5. Leetcode#61 Rotate List

    原题地址 我一直不太理解为什么叫rotate,翻译成"旋转"吧,似乎也不像啊.比如: 1->2->3->4->5->NULL 向右旋转2的距离,变成了 ...

  6. [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 ...

  7. leetcode[61] Unique Paths

    题目:给定一个m*n的矩阵,从头开始,只能往右边和下边走,一次走一格,知道走到最后一个(右下角)为止.总共有多少种走法. 典型的动态规划吧.其实从头走到尾部,和从尾部开始走到头是一样的次数.我们用一个 ...

  8. LeetCode(61)-Valid Palindrome

    题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...

  9. [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 ...

随机推荐

  1. Apache Spark Shark的简介

    Shark是构建在Spark和Hive基础之上的数据仓库. 目前,Shark已经完成学术使命,终止开发,但其架构和原理仍具有借鉴意义. 它提供了能够查询Hive中所存储数据的一套SQL接口,兼容现有的 ...

  2. Hibernate之基于外键映射的一对一(1-1)关联关系

    1.对于基于外键的1-1关联,其外键可以存放在任意一边,在需要存放外键一端,增加many-to-one元素.为many-to-one元素增加unique="true"属性来表示为1 ...

  3. RTMP、RTSP、HTTP视频协议详解(转)

    一.RTMP.RTSP.HTTP协议 这三个协议都属于互联网 TCP/IP 五层体系结构中应用层的协议.理论上这三种都可以用来做视频直播或点播.但通常来说,直播一般用 RTMP.RTSP.而点播用 H ...

  4. 使用friso中文分词注意

    friso是使用c语言开发的一款中文分词器,使用流行的mmseg算法实现.完全基于模块化设计和实现,可以很方便的植入到其他程序中,例如:MySQL,PHP等.目前只支持UTF-8 编码. 源码无需修改 ...

  5. Timus OJ 1997 Those are not the droids you're looking for (二分匹配)

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1997 这个星球上有两种人,一种进酒吧至少玩a小时,另一种进酒吧最多玩b小时. 下面n行是 ...

  6. 开源 免费 java CMS - FreeCMS1.5-信息管理

    下载地址:http://code.google.com/p/freecms/ 信息管理 1. 快速添加 从左侧管理菜单点击快速添加进入. 输入相关属性后添加“保存”按钮即可. 从FreeCMS 1.4 ...

  7. sql server2008添加登录账户配置权限 && 登录时18456错误

    1.如何为SQL Server2008添加登录账户并配置权限 2.SQLSERVER2008 18456错误 http://blog.csdn.net/goodshot/article/details ...

  8. PHP 中运用 elasticsearch

    PHP扩展安装 1. 环境要求:PHP_VERSION >= 5.3.9,composer工具 2. 在E盘新建文件夹命名为elastic,,拷贝composer.phar到      E:/e ...

  9. RealSense 3D实感体验:前景广阔目前应用少

    腾讯数码讯(周硕)在去年的IDF大会上,英特尔着重展示了其全新的RealSense 3D实感技术,而厚度仅6mm堪称史上最薄平板的戴尔Venue 8 7000也成为首个搭载RealSense技术的产品 ...

  10. ListView删除选中的多项目

    //ListView删除选中的多项目function DeleteMultSelItems(ListView:TListView):Boolean;var  I: Integer;begin  Res ...