mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
Merge pull request #426 from pomkarnath98/rotate-list-right
Added Rotate List Right by k places to the list
This commit is contained in:
45
Data-Structures/Linked-List/RotateListRight.js
Normal file
45
Data-Structures/Linked-List/RotateListRight.js
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
/**
|
||||||
|
* A LinkedList based solution for Rotating a List to the right by k places
|
||||||
|
*/
|
||||||
|
|
||||||
|
function main () {
|
||||||
|
/*
|
||||||
|
Problem Statement:
|
||||||
|
Given a linked list, rotate the list to the right by k places, where k is non-negative.
|
||||||
|
|
||||||
|
Note:
|
||||||
|
* While Solving the problem in given link below, don't use main() function.
|
||||||
|
* Just use only the code inside main() function.
|
||||||
|
* The purpose of using main() function here is to aviod global variables.
|
||||||
|
|
||||||
|
Link for the Problem: https://leetcode.com/problems/rotate-list/
|
||||||
|
*/
|
||||||
|
// Reference to both head and k is given in the problem. So please ignore below two lines
|
||||||
|
let head = ''
|
||||||
|
let k = ''
|
||||||
|
let i = 0
|
||||||
|
let current = head
|
||||||
|
while (current) {
|
||||||
|
i++
|
||||||
|
current = current.next
|
||||||
|
}
|
||||||
|
k %= i
|
||||||
|
current = head
|
||||||
|
let prev = null
|
||||||
|
while (k--) {
|
||||||
|
if (!current || !current.next) {
|
||||||
|
return current
|
||||||
|
} else {
|
||||||
|
while (current.next) {
|
||||||
|
prev = current
|
||||||
|
current = current.next
|
||||||
|
}
|
||||||
|
prev.next = current.next
|
||||||
|
current.next = head
|
||||||
|
head = current
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return head
|
||||||
|
}
|
||||||
|
|
||||||
|
main()
|
Reference in New Issue
Block a user