Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 36d0087

Browse files
add q61
1 parent b635995 commit 36d0087

File tree

4 files changed

+73
-30
lines changed

4 files changed

+73
-30
lines changed

‎.idea/workspace.xml‎

Lines changed: 31 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
* [q2_两数相加](/src/链表操作/q2_两数相加)
1212
* [q19_删除链表的倒数第N个节点](/src/链表操作/q19_删除链表的倒数第N个节点)
13+
* [q61_旋转链表](/src/链表操作/q61_旋转链表)
1314
* [q138_复制带随机指针的链表](/src/链表操作/q138_复制带随机指针的链表)
1415
* [q206_反转链表](/src/链表操作/q206_反转链表)
1516

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package 链表操作.q61_旋转链表;
2+
3+
public class ListNode {
4+
int val;
5+
ListNode next;
6+
7+
ListNode(int x) {
8+
val = x;
9+
}
10+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package 链表操作.q61_旋转链表;
2+
3+
/**
4+
* 先连接成环再找断点 o(n)
5+
*/
6+
public class Solution {
7+
8+
public ListNode rotateRight(ListNode head, int k) {
9+
if (head == null) {
10+
return null;
11+
}
12+
if (head.next == null) {
13+
return head;
14+
}
15+
16+
ListNode oldTail = head;
17+
int n;
18+
for (n = 1; oldTail.next != null; n++) {
19+
oldTail = oldTail.next;
20+
}
21+
oldTail.next = head;
22+
ListNode newTail = head;
23+
for (int i = 0; i < n - k % n - 1; i++) {
24+
newTail = newTail.next;
25+
}
26+
ListNode newHead = newTail.next;
27+
newTail.next = null;
28+
29+
return newHead;
30+
}
31+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /