We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Here's my solution in Kotlin. I did it using circular LinkedList.
data class Node(val value: Int, var nextNode: Node?)
fun main(args: Array) {
val (n, d) = readLine()!!.split(" ").map(String::toInt)
val list = readLine()!!.split(" ").map(String::toInt)
var head = Node(list[0], null)
var current = head
var rotatedHead = head
for (i in 1 until n) {
val node = Node(list[i], null)
current.nextNode = node
current = node
if (i == d % n)
rotatedHead = node
}
current.nextNode = head
current = rotatedHead
repeat(n) {
print(current.value.toString() + " ")
current = current.nextNode!!
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Left Rotation
You are viewing a single comment's thread. Return to all comments →
Here's my solution in Kotlin. I did it using circular LinkedList. data class Node(val value: Int, var nextNode: Node?)
fun main(args: Array) { val (n, d) = readLine()!!.split(" ").map(String::toInt) val list = readLine()!!.split(" ").map(String::toInt) var head = Node(list[0], null) var current = head var rotatedHead = head for (i in 1 until n) { val node = Node(list[i], null) current.nextNode = node current = node if (i == d % n) rotatedHead = node } current.nextNode = head
}