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 ...
随机推荐
- homework09-虐心的现程设终于要告一段落了
V3.0版本今天凌晨出炉 添加了随机生成 添加了文件打开 完全按照老师的要求搞定了 V2.0版本更新 添加了中间数组变量显示 这次作业写了整整一天,把以前能用的代码都改了一个遍 最后变成了网页版的小程 ...
- work7
uno. 理解C++变量的作用域和生命周期 没有要求讲解我就简单注释了一下~ #include <iostream>int main(){ for (int i=0;i<10;i++ ...
- Xcode 的正确打开方式——Debugging
程序员日常开发中有大量时间都会花费在 debug 上,从事 iOS 开发不可避免地需要使用 Xcode.这篇博客就主要介绍了 Xcode 中几种能够大幅提升代码调试效率的方式. “If debuggi ...
- HDU 5660 jrMz and angles (暴力枚举)
jrMz and angles 题目链接: http://acm.hust.edu.cn/vjudge/contest/123316#problem/E Description jrMz has tw ...
- [iOS 多线程 & 网络 - 4.0] - AFN框架简单使用
A.AFN基本知识 1.概念 AFNetworking 是对NSURLConnection的封装 运行效率没有ASI高(因为ASI基于CFNetwork),但是使用简单 AFN支持ARC B. ...
- jQuery基础学习6——基本选择器
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- ecshop后台限制IP登录
ecshop是开源系统,所以难免会有漏洞 黑客攻击网站,往往是通过漏洞获取后台管理员权限,然后再做一些破坏 如果我们在后台文件里限制指定的IP才能登录后台,就相对安全多了 下面给出大家解决方案: ...
- Template模式
在开发时,有时会遇到对于一个算法的实现,在不同的对象中有不同的实现,可是这个算法的框架是同样的.这时能够使用Template模式或Strategy模式. Template是採用继承的方式来实现这一点, ...
- hdu 4662 MU Puzzle
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4662 MU Puzzle Time Limit: 2000/1000 MS (Java/Others) ...
- bzoj 4300: 绝世好题 dp
4300: 绝世好题 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.php ...