forked from ecmadao/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNo61.rotate-list.js
More file actions
73 lines (69 loc) · 1.76 KB
/
No61.rotate-list.js
File metadata and controls
73 lines (69 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* Difficulty:
* Medium
*
* Desc:
* Given a list, rotate the list to the right by k places, where k is non-negative.
*
* Example:
* Given 1->2->3->4->5->NULL and k = 2,
* return 4->5->1->2->3->NULL.
*
* 给一个链表和 k,把倒数第 k 个节点和其之后的链表移到原链表的头部
*/
/**
* 思路:
* 没有什么意思的题目,主要考察的是快速定位到链表的第 n 个元素。可以通过双索引来解决
* 该题需要注意的是 k 的取值问题:
* k === 0 || k === list length: 直接返回 head
* k > list length: k = k % list length
*
* Given [0,1,2], rotate 1 steps to the right -> [2,0,1].
* Given [0,1,2], rotate 2 steps to the right -> [1,2,0].
* Given [0,1,2], rotate 3 steps to the right -> [0,1,2].
* Given [0,1,2], rotate 4 steps to the right -> [2,0,1].
* So, no matter how big K, the number of steps is, the result is always the same as rotating K % n steps to the right.
*/
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} k
* @return {ListNode}
*/
var rotateRight = function(head, k) {
if (!head || !head.next || k === 0) return head;
var tmp = head;
var length = 1;
while (tmp.next) {
length += 1;
tmp = tmp.next;
}
if (k === length) return head;
k = k % length;
if (k === 0) return head;
var slow = 1;
var quick = 1;
var node = head;
var slowPre = null;
var slowNode = head;
while(node.next) {
if (quick - slow < k - 1) {
quick += 1;
} else {
quick += 1;
slow += 1;
slowPre = slowNode;
slowNode = slowNode.next;
}
node = node.next;
}
slowPre.next = null;
node.next = head;
return slowNode;
};