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->2->3->4->5->NULL and k = 2,
return 4->5->1->2->3->NULL.
- /*************************************************************************
- > File Name: LeetCode061.c
- > Author: Juntaran
- > Mail: JuntaranMail@gmail.com
- > Created Time: Tue 17 May 2016 18:47:57 PM CST
- ************************************************************************/
- /*************************************************************************
- 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.
- ************************************************************************/
- #include <stdio.h>
- /**
- * Definition for singly-linked list.
- * struct ListNode {
- * int val;
- * struct ListNode *next;
- * };
- */
- struct ListNode* rotateRight(struct ListNode* head, int k)
- {
- struct ListNode* fast = head;
- struct ListNode* slow = head;
- struct ListNode* newhead = head;
- int length = ;
- if( head == NULL || k < )
- {
- return head;
- }
- while( fast->next != NULL )
- {
- fast = fast->next;
- length ++;
- }
- fast = head;
- k = k % length;
- // printf("%d %d\n",k,length);
- while( k > )
- {
- fast = fast->next;
- if( fast == NULL )
- {
- return head;
- }
- k --;
- }
- while( fast->next != NULL )
- {
- slow = slow->next;
- fast = fast->next;
- }
- fast->next = head;
- newhead = slow->next;
- slow->next = NULL;
- return newhead;
- }
LeetCode 61的更多相关文章
- LeetCode: 61. Rotate List(Medium)
1. 原题链接 https://leetcode.com/problems/rotate-list/description/ 2. 题目要求 给出一个链表的第一个结点head和正整数k,然后将从右侧开 ...
- LeetCode 61:旋转链表 Rotate List
给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. Given a linked list, rotate the list to the right by k pla ...
- [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 ...
- Java实现 LeetCode 61 旋转链表
61. 旋转链表 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = ...
- Leetcode#61 Rotate List
原题地址 我一直不太理解为什么叫rotate,翻译成"旋转"吧,似乎也不像啊.比如: 1->2->3->4->5->NULL 向右旋转2的距离,变成了 ...
- [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 ...
- leetcode[61] Unique Paths
题目:给定一个m*n的矩阵,从头开始,只能往右边和下边走,一次走一格,知道走到最后一个(右下角)为止.总共有多少种走法. 典型的动态规划吧.其实从头走到尾部,和从尾部开始走到头是一样的次数.我们用一个 ...
- LeetCode(61)-Valid Palindrome
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- [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 ...
随机推荐
- Apache Spark Shark的简介
Shark是构建在Spark和Hive基础之上的数据仓库. 目前,Shark已经完成学术使命,终止开发,但其架构和原理仍具有借鉴意义. 它提供了能够查询Hive中所存储数据的一套SQL接口,兼容现有的 ...
- Hibernate之基于外键映射的一对一(1-1)关联关系
1.对于基于外键的1-1关联,其外键可以存放在任意一边,在需要存放外键一端,增加many-to-one元素.为many-to-one元素增加unique="true"属性来表示为1 ...
- RTMP、RTSP、HTTP视频协议详解(转)
一.RTMP.RTSP.HTTP协议 这三个协议都属于互联网 TCP/IP 五层体系结构中应用层的协议.理论上这三种都可以用来做视频直播或点播.但通常来说,直播一般用 RTMP.RTSP.而点播用 H ...
- 使用friso中文分词注意
friso是使用c语言开发的一款中文分词器,使用流行的mmseg算法实现.完全基于模块化设计和实现,可以很方便的植入到其他程序中,例如:MySQL,PHP等.目前只支持UTF-8 编码. 源码无需修改 ...
- 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行是 ...
- 开源 免费 java CMS - FreeCMS1.5-信息管理
下载地址:http://code.google.com/p/freecms/ 信息管理 1. 快速添加 从左侧管理菜单点击快速添加进入. 输入相关属性后添加“保存”按钮即可. 从FreeCMS 1.4 ...
- sql server2008添加登录账户配置权限 && 登录时18456错误
1.如何为SQL Server2008添加登录账户并配置权限 2.SQLSERVER2008 18456错误 http://blog.csdn.net/goodshot/article/details ...
- PHP 中运用 elasticsearch
PHP扩展安装 1. 环境要求:PHP_VERSION >= 5.3.9,composer工具 2. 在E盘新建文件夹命名为elastic,,拷贝composer.phar到 E:/e ...
- RealSense 3D实感体验:前景广阔目前应用少
腾讯数码讯(周硕)在去年的IDF大会上,英特尔着重展示了其全新的RealSense 3D实感技术,而厚度仅6mm堪称史上最薄平板的戴尔Venue 8 7000也成为首个搭载RealSense技术的产品 ...
- ListView删除选中的多项目
//ListView删除选中的多项目function DeleteMultSelItems(ListView:TListView):Boolean;var I: Integer;begin Res ...