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 dce9fe8

Browse files
Merge pull request doocs#220 from huaxu1024/master
Create Solution.java
2 parents 27975ed + 8c1dd85 commit dce9fe8

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode rotateRight(ListNode head, int k) {
11+
if (head == null)
12+
return head;
13+
if (head.next == null)
14+
return head;
15+
16+
int size = 1;
17+
// build ring
18+
ListNode nodeLast = head;
19+
while (nodeLast.next != null) {
20+
nodeLast = nodeLast.next;
21+
size++;
22+
}
23+
nodeLast.next = head;
24+
25+
// cutting
26+
k = size - k % size;
27+
while (k-- > 0) {
28+
nodeLast = head;
29+
head = head.next;
30+
}
31+
nodeLast.next = null;
32+
return head;
33+
}
34+
}

0 commit comments

Comments
(0)

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