-
Notifications
You must be signed in to change notification settings - Fork 1
/
LC51-旋转链表.js
83 lines (80 loc) · 2.02 KB
/
LC51-旋转链表.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* @Author: Ran
* @Date: 2021-03-27 14:33:07
* @LastEditTime: 2021-03-28 10:34:19
* @FilePath: \JZoffer\LeetCode\LC51-旋转链表.js
* @Description:
* 给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置。
*
* 输入:head = [1,2,3,4,5], k = 2
* 输出:[4,5,1,2,3]
*
* 输入:head = [0,1,2], k = 4
* 输出:[2,0,1]
*/
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @param {number} k
* @return {ListNode}
*/
var rotateRight = function(head, k) {
if (!head) return head;
// 首先看看这个链表一共有几个节点 同时记住尾节点 tail
let node = head;
let tail = null;
let count = 0;
while (node) {
count++;
tail = node;
node = node.next;
}
// 然后节点数跟 k 取余
k = k % count;
// 余数为 0 的话直接返回原来的链表即可
if (k === 0) return head;
// 头尾相连:
tail.next = head;
node = head;
// 根据余数来截链表
let cutNode = null;
for (let i = 0; i < count - k; i++) {
cutNode = node;
node = node.next;
}
let newHead = cutNode.next;
cutNode.next = null;
return newHead;
};
var rotateRight = function(head, k) {
if (!head) return head;
// 首先看看这个链表一共有几个节点 同时记住尾节点 tail
let node = head;
let tail = null;
let count = 0;
while (node) {
count++;
tail = node;
node = node.next;
}
// 然后节点数跟 k 取余
k = k % count;
// 余数为 0 的话直接返回原来的链表即可
if (k === 0) return head;
// 头尾相连:
tail.next = head;
node = head;
// 根据余数来截链表
for (let i = 1; i < count - k; i++) {
node = node.next;
}
let newHead = node.next;
node.next = null;
return newHead;
};