LeetCode OJ: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.
将链表从右边数第k个开始后面的全部交换到链表的左侧。例如对于1,2,3,4,5,6 和 k = 2,就有交换之后的节点就是5,6,1,2,3,4。注意这里的k可能会超过链表的长度大小。
做法是首先可以计算链表的长度len,再将链表连成一个环状。接着从链表起始数len - k将链表断开,那么就完成了旋转,代码如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
if(!head) return NULL;
ListNode * p1 = head;
int count = ;
while(p1->next){
count++;
p1 = p1->next;
}
count++;
int len = count - k%count;//注意给出的k可能会超过链表的总长度
if(len == )
return head;
p1->next = head;
p1 = head;
for(int i = ; i < len; ++i){
p1 = p1->next;
}
ListNode * ret = p1->next;
p1->next = NULL;
return ret;
}
};
LeetCode OJ:Rotate List(旋转链表)的更多相关文章
- [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旋转链表
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 ...
- 【python】Leetcode每日一题-旋转链表
[python]Leetcode每日一题-旋转链表 [题目描述] 给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置. 示例1: 输入:head = [1,2,3,4,5] ...
- [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)
目录 描述 解法:双指针 思路 Java 实现 Python 实现 复杂度分析 描述 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1-> ...
- Leetcode61. Rotate List旋转链表
给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = 2 输出: 4-& ...
- leetcode刷题-61旋转链表
题目 给定一个链表,旋转链表,将链表每个节点向右移动 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] 189. Rotate Array 旋转数组
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
随机推荐
- springboot 监控
一.什么是spring-boot-starter-actuator(doc) springboot项目如何检查配置与运行状态呢?官方提供了一些接口可以查看springboot项目运行情况,只需要导入s ...
- Python3.x:常用基础语法
Python3.x:常用基础语法 1,if else语句: 不执行if内的语句,需要用:pass if i>2: #跳过不执行 pass else: print("i= %s" ...
- 20145314郑凯杰 《Java程序设计》第4周学习总结
20145314郑凯杰 <Java程序设计>第4周学习总结 所有代码已上传: 教材学习内容总结 ①继承 设计程序中,因们需要设计多个模块,我想到了李晓东以前教我们的三个字"模块化 ...
- shell编程(一)
转义和引用 引入问题:之前我们知道了变量名前面加上$符号代表引用变量,但是如果我现在就需要打印出$符号该怎么办呢?想想我们在python中怎么做的,答案是转义. 转义 Shell中有两种字符一种是普通 ...
- SpringBoot 集成Netty实现UDP Server
注:ApplicationRunner 接口是在容器启动成功后的最后一步回调(类似开机自启动). UDPServer package com.vmware.vCenterEvent.netty; im ...
- IntelliJ IDEA 开发git多模块项目
1.clone主项目 填写主仓库地址 2.在项目根目录,初始化子模块,并clone源码 git submodule init git submodule update 3.定位到各个子模块根目录,并切 ...
- springboot部署在云服务器上
1.window云服务器上 在本地的SpringBoot的根目录下 mvn clean package 打包jar 在云服务上安装jdk 将jar拷贝到云服务器上 在jar包所在的相应的位置,执行ja ...
- mybatis映射文件_select_resultMap
实体类: Employee.java类: package com.hand.mybatis.bean; public class Employee { private Integer e ...
- 2017 beijing icpc A - Euler theorem
2017-09-22 21:59:43 writer:pprp HazelFan is given two positive integers a,ba,b, and he wants to calc ...
- 纯js提交get和post请求
get function get(URL, PARAMS) { var temp = document.createElement("form"); temp.method = & ...