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 c6fe792

Browse files
author
kkarpyshev
committed
Medium61 challenge
1 parent c41da11 commit c6fe792

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

‎src/medium/61. Rotate List .kt‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package medium
2+
3+
import LinkedListTopic
4+
import TwoPointersTopic
5+
import utils.ListNode
6+
7+
/**
8+
* 61. Rotate List
9+
* https://leetcode.com/problems/rotate-list/
10+
*
11+
Given the head of a linked list, rotate the list to the right by k places.
12+
*/
13+
14+
class Medium61 : LinkedListTopic, TwoPointersTopic {
15+
16+
fun rotateRight(head: ListNode?, k: Int): ListNode? {
17+
val list = ArrayList<ListNode>()
18+
var node = head
19+
while (node != null) {
20+
list.add(node)
21+
node = node.next
22+
}
23+
val sentinel = ListNode(0)
24+
node = sentinel
25+
for (i in list.indices) {
26+
node?.next = list[(i - (k % list.size) + list.size) % list.size]
27+
node = node?.next
28+
}
29+
node?.next = null
30+
return sentinel.next
31+
}
32+
}

0 commit comments

Comments
(0)

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