-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path061.js
More file actions
37 lines (35 loc) · 718 Bytes
/
061.js
File metadata and controls
37 lines (35 loc) · 718 Bytes
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
/**
* 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 (k === 0) return head;
let temp = head;
let length = 0;
let list = [];
while(temp) {
list.push(temp);
temp = temp.next;
++length;
}
let lastNode = list[length - 1];
if (length === 0) return head;
while(k > length) {
k = k - length;
}
if (k === length) return head;
let left = length - k - 1;
let leftNode = list[left];
let leftNextNode = list[left + 1];
lastNode.next = head;
leftNode.next = null;
return leftNextNode;
};