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的更多相关文章

  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. 执行原始的 SQL 查询

    The Entity Framework Code First API includes methods that enable you to pass SQL commands directly t ...

  2. 第二百五十一天 how can I 坚持

    hadoop,namenote和datanode.namenode如果要是在启动时加载到内存,会不会对内存的要求比较高呢. edits-->fsimage. secondnamenode,那么n ...

  3. PDB符号文件信息

    一.前言 这个方法是通过网上的一些方式自己学习枚举PDB文件信息. 二.代码实现 首先枚举驱动文件,这里用psapi库 #include "psapi.h" #pragma com ...

  4. mockjs学习总结(方便前端模拟数据,加快开发效率)

      基本介绍: 在我们前端开发中经常遇到这样的事情,接口没有写好,只能写静态页面,如何才能用很简单的方法模拟后端数据呢?mockjs就干了这件事,而且干的还挺好. 下面是我作为初学者的一些总结经验,期 ...

  5. iOS有关截图的操作

    1.截取选中view的图片 //根据size大小创建一个基于位图的图形上下文 CGRect rect =view.frame; UIGraphicsBeginImageContext(rect.siz ...

  6. 从零开始学android开发-setBackgroundDrawable与setBackgroundResource的区别

    setBackgroundDrawable和setBackgroundResource的区别很多网友不知道View类提供的setBackgroundDrawable和setBackgroundReso ...

  7. BW知识点总结及面试要点

    1.       如何理解数据仓库? 数据仓库 是 一个面向主题的,集成的,相对稳定的,反应历史变化的数据集合,用于支持管理决策. 2.       OLAP 和 OLTP的基本概念 和 区别? Ol ...

  8. Codeforces Beta Round #85 (Div. 1 Only) A. Petya and Inequiations 贪心

    A. Petya and Inequiations Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...

  9. Codeforces Round #274 (Div. 1) C. Riding in a Lift 前缀和优化dp

    C. Riding in a Lift Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/pr ...

  10. DWRUtil is not defined

    错误:DWRUtil is not defined; 解决方法: 先检查页面中是否正确引入uitl.js <script type="text/javascript" src ...