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 2a24eb3

Browse files
Create Solution.java
1 parent 27975ed commit 2a24eb3

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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) return head;
12+
if (head.next == null) return head;
13+
14+
int size = 1;
15+
// build ring
16+
ListNode nodeLast = head;
17+
while (nodeLast.next!=null) {
18+
nodeLast = nodeLast.next;
19+
size++;
20+
}
21+
nodeLast.next = head;
22+
23+
// cutting
24+
k = size-k%size;
25+
while (k-->0){
26+
nodeLast = head;
27+
head = head.next;
28+
}
29+
nodeLast.next = null;
30+
return head;
31+
}
32+
}

0 commit comments

Comments
(0)

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