LeetCode进阶之路(Rotate List)
来源:互联网 发布:烈火封神翅膀进阶数据 编辑:程序博客网 时间:2024/05/01 12:59
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个node,从那开始到结尾和之前那部分对调,那个例子就是,4->5拿前面来,1->2->3拿后面去。
思路:我自己是用了三个指针,分别是移到k位置,第一位,然后把三段拼起来。思路是正确,但是太复杂,绕来绕去,后面提交也是超时了。
public ListNode rotateRight(ListNode head, int k) {ListNode result = new ListNode(-1);result.next = head;ListNode first = result;ListNode second = head;ListNode third = result;if(head == null) {return result.next;}if(head.next == null){return head;}int len = 1;while(result.next != null) {len++;result = result.next;}int i = 0;while(i < len-k) {i++;first = first.next;}third.next = first;int j = 1;while(j < len-1-k) {second = second.next;j++;}second.next = null;ListNode flag = third;while(flag.next != null) {flag = flag.next;}flag.next = head;return third.next;}
下面是参考网友的思路,用两个指针,faster/slower,先对faster设步长为n,然后faster和slower再一起走,知道faster.next==null,说明slower指向要倒着数的开始点的前一个位置。
public ListNode rotateRight(ListNode head, int n) { if(head==null||head.next==null||n==0) return head; ListNode fast = head, slow = head,countlen = head; ListNode newhead = new ListNode(-1); int len = 0; while(countlen!=null){ len++; countlen = countlen.next; } n = n%len; if(n==0) return head; for(int i = 0; i < n; i++) fast = fast.next; while(fast.next!=null){ slow = slow.next; fast = fast.next; } newhead = slow.next; fast.next = head; slow.next = null; return newhead; }还有一种更简单的思路,把整个链表连成一个环,在重新分割就可以了。
public ListNode rotateRight(ListNode head, int n) { if (head == null || n == 0) return head; ListNode p = head; int len = 1;//since p is already point to head while (p.next != null) { len++; p = p.next; } p.next = head; //form a loop n = n % len; for (int i = 0; i < len - n; i++) { p = p.next; } //now p points to the prev of the new head head = p.next; p.next = null; return head;}
0 0
- LeetCode进阶之路(Rotate List)
- LeetCode进阶之路(Rotate Image)
- leetcode之Rotate List
- LeetCode之Rotate List
- leetcode之Rotate List
- LeetCode之Rotate List
- leetcode之Rotate List
- 【Leetcode】之Rotate List
- leetcode:单链表之Rotate List
- Leetcode之Rotate List 问题
- Leetcode进阶之路——Rotate Image
- LeetCode(61) Rotate List
- leetcode 刷题之路 13 Rotate List
- LeetCode OJ 之 Rotate List (链表旋转)
- [leetcode][list] Rotate List
- leetcode 之Rotate List 解题思路
- LeetCode 之 Rotate List — C 实现
- LeetCode 题解(37): Rotate List
- Vijos P1008 篝火晚会(组合数学,置换群)
- Leetcode 213. House Robber II (Medium) (cpp)
- java8之Stream语法详解
- poj2533
- 2015年最新BigD牛魔王UI设计视频教程
- LeetCode进阶之路(Rotate List)
- 移动端web页面开发中遇到的三个“小坑”
- react on es6+
- Linux下TCP编程的注意事项-陈硕《网络编程实战》总结
- solr初用 修改配置
- nexus 3 maven 私服搭建
- {分享}《Matrix67:趣题:非常具有启发性的概率问题》
- Android 拍照或从相册取图片并裁剪
- 关于javascript "use strict"的学习