061 Rotate List 旋转链表
给定一个链表,将链表向右旋转 k 个位置,其中 k 是非负数。
示例:
给定 1->2->3->4->5->NULL 且 k = 2,
返回 4->5->1->2->3->NULL.
详见:https://leetcode.com/problems/rotate-list/description/
Java实现:
- /**
- * Definition for singly-linked list.
- * public class ListNode {
- * int val;
- * ListNode next;
- * ListNode(int x) { val = x; }
- * }
- */
- class Solution {
- public ListNode rotateRight(ListNode head, int k) {
- if(head==null||k<=0){
- return head;
- }
- int len=0;
- ListNode cur=head;
- while(cur!=null){
- ++len;
- cur=cur.next;
- }
- k%=len;
- ListNode slow=head;
- ListNode fast=head;
- for(int i=0;i<k;++i){
- if(fast.next!=null){
- fast=fast.next;
- }else{
- return head;
- }
- }
- while(fast.next!=null){
- slow=slow.next;
- fast=fast.next;
- }
- fast.next=head;
- fast=slow.next;
- slow.next=null;
- return fast;
- }
- }
061 Rotate List 旋转链表的更多相关文章
- [LeetCode] 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. Rotate List 旋转链表
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
- Leetcode61. Rotate List旋转链表
给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = 2 输出: 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 ...
- [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 ...
- 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 ...
- [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 ...
- 旋转链表(所有元素往右移) rotate list
[抄题]: 给定一个链表,旋转链表,使得每个节点向右移动k个位置,其中k是一个非负数 样例 给出链表1->2->3->4->5->null和k=2 返回4->5-& ...
- LeetCode 61:旋转链表 Rotate List
给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. Given a linked list, rotate the list to the right by k pla ...
随机推荐
- linux命令学习笔记(25):linux文件属性详解
Linux 文件或目录的属性主要包括:文件或目录的节点.种类.权限模式.链接数量.所归属的用户和用户组. 最近访问或修改的时间等内容.具体情况如下: 命令: ls -lih 输出: [root@loc ...
- HihoCoder1333 :平衡树(splay+lazy)(区间加值,区间删除)
描述 小Ho:好麻烦啊~~~~~ 小Hi:小Ho你在干嘛呢? 小Ho:我在干活啊!前几天老师让我帮忙管理一下团队的人员,但是感觉好难啊. 小Hi:说来听听? 小Ho:事情是这样的.我们有一个运动同好会 ...
- P2762 [网络流24题]太空飞行计划问题(最小割)
地址 最大权闭合子图裸题,不说了吧,求方案就是把s集遍历一遍. 错误记录:dfs那块忘判断残量了,11分×1. #include<cstdio> #include<iostream& ...
- 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 ...
- dubbo设计实现的健壮性
Dubbo 作为远程服务暴露.调用和治理的解决方案,是应用运转的经络,其本身实现健壮性的重要程度是不言而喻的. 这里列出一些 Dubbo 用到的原则和方法. 日志 日志是发现问题.查看问题一个最常用的 ...
- C# 获取Console的输入和输出 数据 (异步)
using System ; using System .Diagnostics; using System .IO; class Program { static void Main() ...
- XmlSerialize error: There was an error generating the XML document.
今天遇到一个很火的问题, 一个c#的class 序列化成xml后抛出异常, 信息为: XmlSerialize error: There was an error generating the XML ...
- WPF Get Multibinding Expression, Update Source,
wpf 拿到某个control的multibinding以及其中每个Binding 1. 拿到multibinding MultiBindingExpression mbe = Bindin ...
- SpringMVC之四:渲染Web视图
理解视图解析 在前面的例子中,我们看到控制器返回的都是一个逻辑视图的名称,然后把这个逻辑视图名称交给view resolver,然后返回渲染后的 html 页面给 client. 将控制器中请求处理的 ...
- node.js setup wizard ended prematurely Win7安装nodejs失败解决方法
笔记本win7在nodejs官方网站下载.msi文件安装,安装到一半的时候,进度条提示:roll back,because of a error.node.JS setup wizard ended ...