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 ...
随机推荐
- 理解多线程中的ManualResetEvent(C#)
线程是程序中的控制流程的封装.你可能已经习惯于写单线程程序,也就是,程序在它们的代码中一次只在一条路中执行.如果你多弄几个线程的话,代码运行可能会更加“同步”.在一个有着多线程的典型进程中,零个或更多 ...
- Ajax处理后台返回的Json数据
// 处理后台传来的Json字符串装换成Json对象 var dataJson = JSON.parse(data); // 此时可以从Json对象中取值 if(dataJson.result == ...
- oracle11g登录等问题
一.oracle 11g登录服务开启 成功安装Oracle 11g后,共有7个服务,这七个服务的含义分别为:1. Oracle ORCL VSS Writer Service:Oracle卷映射拷贝写 ...
- Early Media and Music on Hold
Early media refers to any media that is played to the initial caller’s phone before the remote party ...
- ACM学习历程—NPU 2015年陕西省程序设计竞赛网络预赛(正式赛)F题 和谐的比赛(递推)
Description 今天西工大举办了一场比赛总共有m+n人,但是有m人比较懒没带电脑,另外的n个人带了电脑.不幸的是,今天机房的电脑全坏了只能用带的电脑,一台电脑最多两人公用,确保n>=m. ...
- bzoj 3376 [Usaco2004 Open]Cube Stacking 方块游戏——带偏移量的并查集
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3376 带偏移量的并查集. #include<iostream> #include ...
- bzoj 4883 [Lydsy1705月赛]棋盘上的守卫——并查集(思路!)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4883 把各行和各列看成n+m个点. 如果一下能防守行和列,就是最大匹配了.这是每两个左右部点 ...
- 重学JAVA基础(五):面向对象
1.封装 import java.util.Date; public class Human { protected String name; protected BirthDay birthDay; ...
- __setup宏简介
内核组件用__setup宏来注册关键字及相关联的处理函数,__setup宏在include/linux/init.h中定义,其原型如下: __setup(string, fun ...
- vue 之 折线图挤压
当tab标签栏变动时,echarts图会发生挤,这种情况,发现是html容器的width=100%这个设置,变成了width=100px. 解决方式: 1.设置一个最小宽度,获取width的值,当这个 ...