【一天一道LeetCode】#61. Rotate List
一天一道LeetCode系列
(一)题目
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.
(二)解题
本题的思路:
1、找到倒数第K个节点
2、将k以后的节点移动到前面来,与头结点相连。
3、新的头结点就是倒数第k个节点。
需要注意一下几种特殊情况:
1、链表为空或者链表只有一个节点
2、k的值为0或者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(k==0||head == NULL || head->next==NULL) return head;
ListNode* pkth = head;//记录倒数第k个节点
ListNode* pLast = head;//记录最后一个节点
ListNode* ptemp =NULL;//倒数第K+1个节点
ListNode* p = head;
int length = 0;
while(p!=NULL)//求出链表的长度
{
p=p->next;
++length;
}
k %=length;//保证k在0~length之间
if(k==0) return head;//k等于0直接返回head
while(pLast != NULL && --k) pLast = pLast->next;//找到正数第K个节点
while(pLast->next !=NULL){//找到倒数第K个节点
ptemp = pkth;//这里用到两个指针,pLast和pkth同时移动,最后pkth就是倒数第K个节点
pkth = pkth->next;
pLast = pLast->next;
}
ptemp->next = NULL;
pLast->next = head;//调整链表
return pkth;
}
};
【一天一道LeetCode】#61. 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
原题地址 我一直不太理解为什么叫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. 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(Medium)
1. 原题链接 https://leetcode.com/problems/rotate-list/description/ 2. 题目要求 给出一个链表的第一个结点head和正整数k,然后将从右侧开 ...
- 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 ...
- [leetcode]61. Rotate List反转链表k个节点
类似于找链表的后k个节点 不同的是要把前边的接到后边 public ListNode rotateRight(ListNode head, int k) { //特殊情况 if (head==null ...
- 【一天一道LeetCode】#48. Rotate Image
一天一道LeetCode系列 (一)题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 ...
- 【LeetCode】61. Rotate List 解题报告(Python)
[LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
随机推荐
- rhel7 配置普通用户使用sudo
rhel服务器版本安装之后,默认创建的用户不能使用sudo.使用sudo,会提示 user1 is not in the sudoers file. This incident will be rep ...
- Mac下配置远程Linux 服务器SSH密钥认证自动登录
1. 在本地机器创建公钥 打开万能的终端,执行如下命令,无视一切输出,一路欢快地回车即可. ssh-keygen -t rsa -C 'your email@domain.com' -t 指定密钥类型 ...
- comtypes加word 2013批量将pdf转换为doc
office 2013很强大. import os import sys import re import comtypes.client wdFormatPDF = 17 def covx_to_p ...
- git 撤销没有提交的变化
参考: https://stackoverflow.com/questions/5807137/how-to-revert-uncommitted-changes-including-files-an ...
- 聚沙成塔-linux 常用命令
批量更改文件后缀名 find . -depth -name "*.scss" -exec sh -c 'mv "$1" "${1%.scss}.les ...
- 2016年年终CSDN博客总结
2015年12月1日,结束了4个月的尚观嵌入式培训生涯,经过了几轮重重面试,最终来到了伟易达集团.经过了长达3个月的试用期,正式成为了伟易达集团的助理工程师. 回顾一年来的学习,工作,生活.各种酸甜苦 ...
- IntelliJ Idea 设置 Dialyzer
IntelliJ Idea 设置 Dialyzer(金庆的专栏)Erlang开发使用IDEA IDE可以设置外部工具Dialyzer, 然后就可以直接Tools->External Tools ...
- win 8.1 64位彻底删除王码98
安全模式 C:\Windows\SysWOW64\ 删除:winwb98.IME和winwb98.MB 注册表: 找到王码五笔相关的项.一般在最下面的以字母E开头的文件夹.全部删除. HKEY_LOC ...
- java创建线程
创建一个线程 Java提供了两种创建线程方法: 通过实现Runable接口: http://blog.csdn.net/duruiqi_fx/article/details/52187275 通过继承 ...
- java泛型总结(类型擦除、伪泛型、陷阱)
JDK1.5开始实现了对泛型的支持,但是java对泛型支持的底层实现采用的是类型擦除的方式,这是一种伪泛型.这种实现方式虽然可用但有其缺陷. <Thinking in Java>的作者 B ...