旋转链表(所有元素往右移) rotate list
[抄题]:
给定一个链表,旋转链表,使得每个节点向右移动k个位置,其中k是一个非负数
样例
给出链表1->2->3->4->5->null和k=2
返回4->5->1->2->3->null
[思维问题]:
就是两条线段的变种,想不到联系
[一句话思路]:
用线段找到位置n,然后连一下
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
连接的时候:
head.next = dummy.next;
dummy.next = tail;(错了)
1->2->3->2->1->null
1
wrong:
2->null
right:
1->1->2->3->2->null
[二刷]:
- 因为 head = dummy;往前移动了一位,快指针退出的条件是后一位非空while(head.next != null)
- tail = dummy;慢指针一定要初始化为dummy
[总结]:if (head == null) {
return null;
}
头节点判断空没写就全错了,自己掂量着办吧
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构,为什么不用别的数据结构]:
[其他解法]:
[Follow Up]:
[题目变变变]:
Search in Rotated Sorted Array, 在重复或者不重复的旋转数组中找数,或者求恢复。本题只讨论旋转数组是怎么制造出来的。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/ public class Solution {
/*
* @param head: the List
* @param k: rotate to the right k places
* @return: the list after rotation
*/
//calculate
private int getLength(ListNode head) {
if (head == null) {
return 0;
}
int index = 0;
while(head != null) {
index++;
head = head.next;
}
return index;
} public ListNode rotateRight(ListNode head, int k) {
if (head == null) {
return null;
}// //find n
int length = getLength(head);
k = k % length;
ListNode dummy = new ListNode(0);
dummy.next = head;
head = dummy;
ListNode tail = dummy;
for(int i = 0; i < k; i++){
head = head.next;
}
//ListNode tail = dummy;
while(head.next != null) {
head = head.next;
tail = tail.next;
}
//join up
head.next = dummy.next;
dummy.next = tail.next;
tail.next = null; return dummy.next;
}
}
旋转链表(所有元素往右移) 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 ...
- [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 ...
- LeetCode 61:旋转链表 Rotate List
给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. Given a linked list, rotate the list to the right by k pla ...
- LeetCode 61. 旋转链表(Rotate List)
题目描述 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = 2 输出 ...
- Leetcode61. Rotate List旋转链表
给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = 2 输出: 4-& ...
- LeetCode(61):旋转链表
Medium! 题目描述: 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, ...
- PAT 乙级 1008 数组元素循环右移问题 (20) C++版
1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数,在不允 ...
- 【LeetCode题解】61_旋转链表(Rotate-List)
目录 描述 解法:双指针 思路 Java 实现 Python 实现 复杂度分析 描述 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1-> ...
- 【PAT】1008. 数组元素循环右移问题 (20)
1008. 数组元素循环右移问题 (20) 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1……AN- ...
随机推荐
- Fragment之介绍(转)
http://www.cnblogs.com/plokmju/p/3239265.html 前言 开门见山开篇名义,本篇博客将讲解一下Android中Fragment的内容,必要的地方会提供相应的演示 ...
- 初步认识AutoMapper
AutoMapper 初步认识AutoMapper 前言 手动映射 使用AutoMapper 创建映射 Conventions 映射到一个已存在的实例对象 前言 通常在一个应用程序中,我们开发 ...
- C++多线程同步之事件(Event)
原文链接:http://blog.csdn.net/olansefengye1/article/details/53291074 一.事件(Event)原理解析 1.线程同步Event,主要用于线程间 ...
- 哈希与位图(Hash and BitMap)
Hash:哈希机制 BitMap:位图机制 目的:都是为了保证检索方便而设置的数据结构 对于大数据进行排序,由于内存限制,不可能在内存中进行,所以采取BitMap机制 为了在大数据中快速检索以及操作数 ...
- java编译器
编译: .java变成.class 前端编译 Sun javac Eclipse ECJ .class变成机器码 运行期编译 等HostSpot VM c1,c2 .jav ...
- 给iOS开发新手送点福利,简述UITableView的属性和用法
UITableView UITableView内置了两种样式:UITableViewStylePlain,UITableViewStyleGrouped <UITableViewDataSo ...
- JavaScript中判断函数、变量是否存在
转载:http://www.jb51.net/article/67551.htm 一.是否存在指定函数 function isExitsFunction(funcName) { try { if (t ...
- Mysql-两表的连接,copy表,select的各种用法
-- 连接:外连接,内连接 两个表之间 外连接:right join left join -- left join 左标为主 一般以值少的为主 select * from table1 left ...
- 玩转laravel5.4的入门动作(二)
做个文章的增删改查 第一步 把数据库的表结构建好,生成迁移 1 怎么建,当然是用php artisan命令了 使用 Artisan 命令 make:migration 来创建一个新的迁移: php ...
- linux添加计划任务
crond 是linux用来定期执行程序的命令.当安装完成操作系统之后,默认便会启动此任务调度命令.crond命令每分锺会定期检查是否有要执行的工作,如果有要执行的工作便会自动执行该工作.可以用以下的 ...