-
Notifications
You must be signed in to change notification settings - Fork 30
/
rotate-list.js
65 lines (58 loc) · 1.26 KB
/
rotate-list.js
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
// 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.
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} k
* @return {ListNode}
*/
var reverseList = function (head) {
var prev = null;
var next = null;
while (head) {
var temp = head.next;
prev = head;
prev.next = next;
next = prev;
head = temp;
}
return prev;
}
var rotateRight = function(head, k) {
if (!head) return null;
var count = 1;
var fakeHead = head;
while (head.next) {
count += 1;
head = head.next;
}
if (k > count) {
k = k % count;
}
var target = count - k;
if (target <= 0) {
return fakeHead;
}
head.next = fakeHead;
head = head.next;
count = 1;
var tail = null;
while (head) {
if (count === target) {
tail = head.next;
head.next = null;
}
count += 1;
head = head.next;
}
return tail;
};