-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
155 lines (151 loc) · 3.45 KB
/
index.js
File metadata and controls
155 lines (151 loc) · 3.45 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* @lc app=leetcode id=61 lang=javascript
*
* [61] Rotate List
*
* https://leetcode.com/problems/rotate-list/description/
*
* algorithms
* Medium (26.33%)
* Total Accepted: 176.9K
* Total Submissions: 669.1K
* Testcase Example: '[1,2,3,4,5]\n2'
*
* Given a linked list, rotate the list to the right by k places, where k is
* non-negative.
*
* Example 1:
*
*
* Input: 1->2->3->4->5->NULL, k = 2
* Output: 4->5->1->2->3->NULL
* Explanation:
* rotate 1 steps to the right: 5->1->2->3->4->NULL
* rotate 2 steps to the right: 4->5->1->2->3->NULL
*
*
* Example 2:
*
*
* Input: 0->1->2->NULL, k = 4
* Output: 2->0->1->NULL
* Explanation:
* rotate 1 steps to the right: 2->0->1->NULL
* rotate 2 steps to the right: 1->2->0->NULL
* rotate 3 steps to the right: 0->1->2->NULL
* rotate 4 steps to the right: 2->0->1->NULL
*
*/
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
const { make_list_node } = require('../utils');
const test1 = make_list_node([0,1,2]);
/**思路一:
* 1.先调转整个链表
* 2.调转前链表前k个
* 3.调转链表后面所有选项
*
* @param {ListNode} head
* @param {number} k
* @return {ListNode}
*/
var rotateRight = function(head, k) {
const length = getListLength(head);
k = k % length;
if (k === 0 || !head) {
return head;
}
const reverseHead = reverse(head);
const {head:result, lastNode, nextNode} = reverseK(reverseHead, k);
lastNode.next = reverse(nextNode);
return result;
function reverse(head) {
let cur = head;
let lastNode = null;
while (cur) {
const currentNext = cur.next;
cur.next = lastNode;
lastNode = cur;
cur = currentNext;
}
return lastNode;
}
function reverseK(head, k) {
let cur = head;
let lastNode = null;
while (k > 0) {
const currentNext = cur.next;
cur.next = lastNode;
lastNode = cur;
cur = currentNext;
k--;
}
return {
head:lastNode,
nextNode:cur,
lastNode:head,
};
}
function getListLength(node) {
let length = 0;
while (node) {
length ++;
node = node.next;
}
return length;
}
};
/**
* 思路2:
* 将链表最后k个插入到链表的前面
* @param {*} head
* @param {*} k
*/
var rotateRight = function(head, k) {
const length = getListLength(head);
k = k % length;
if (k === 0 || !head) {
return head;
}
let lastKNode = head;
let i = length - k;
while (i > 1) {
lastKNode = lastKNode.next;
i--;
}
const result = lastKNode.next;
getLastNode(lastKNode).next = head;
lastKNode.next = null;
return result;
function getLastNode(head) {
let cur = head;
while (cur.next) {
cur = cur.next;
}
return cur;
}
function getListLength(node) {
let length = 0;
while (node) {
length ++;
node = node.next;
}
return length;
}
};
let testNode = rotateRight(test1, 2);
while(testNode) {
console.log(testNode.val);
testNode = testNode.next;
}
module.exports = {
id:'61',
title:'Rotate List',
url:'https://leetcode.com/problems/rotate-list/description/',
difficulty:'Medium',
}