Skip to content

Commit ca0ee44

Browse files
committed
leetcode_0061_Rotate_List
1 parent 5bf6f4f commit ca0ee44

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

leetcode_solved/[editing]leetcode_0061_Rotate_List.cpp

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// AC
2+
class Solution {
3+
public:
4+
ListNode* rotateRight(ListNode* head, int k) {
5+
if (!head) return head;
6+
int len = 1;
7+
ListNode *newH, *tail;
8+
newH = tail = head;
9+
10+
// 获取list 长度
11+
while (tail->next) {
12+
tail = tail->next;
13+
len++;
14+
}
15+
16+
tail->next = head;
17+
18+
// rotate (len - (k % len)) 次
19+
if (k %= len) {
20+
for (int i = 0; i < len - k; i++) {
21+
tail = tail->next;
22+
}
23+
}
24+
25+
newH = tail->next;
26+
tail->next = NULL;
27+
return newH;
28+
}
29+
};

0 commit comments

Comments
 (0)