mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 02:04:31 +08:00
Add Rotate a Linkedlist (#4345)
This commit is contained in:

committed by
GitHub

parent
a96ad84fac
commit
09950d6097
@ -0,0 +1,33 @@
|
|||||||
|
package com.thealgorithms.datastructures.lists;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rotate a list
|
||||||
|
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class RotateSinglyLinkedLists {
|
||||||
|
public Node rotateRight(Node head, int k) {
|
||||||
|
if (head == null || head.next == null || k == 0) {
|
||||||
|
return head;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node curr = head;
|
||||||
|
int len = 1;
|
||||||
|
while (curr.next != null) {
|
||||||
|
curr = curr.next;
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
|
||||||
|
curr.next = head;
|
||||||
|
k = k % len;
|
||||||
|
k = len - k;
|
||||||
|
while (k > 0) {
|
||||||
|
curr = curr.next;
|
||||||
|
k--;
|
||||||
|
}
|
||||||
|
|
||||||
|
head = curr.next;
|
||||||
|
curr.next = null;
|
||||||
|
return head;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,72 @@
|
|||||||
|
package com.thealgorithms.datastructures.lists;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test cases for RotateSinglyLinkedLists
|
||||||
|
* Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
|
||||||
|
*/
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class RotateSinglyLinkedListsTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRotateRightEmptyList() {
|
||||||
|
RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists();
|
||||||
|
|
||||||
|
// Test case: Rotate an empty list
|
||||||
|
assertNull(rotator.rotateRight(null, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRotateRightSingleNodeList() {
|
||||||
|
RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists();
|
||||||
|
|
||||||
|
// Test case: Rotate a list with one element
|
||||||
|
Node singleNode = new Node(5);
|
||||||
|
Node rotatedSingleNode = rotator.rotateRight(singleNode, 3);
|
||||||
|
assertEquals(5, rotatedSingleNode.value);
|
||||||
|
assertNull(rotatedSingleNode.next);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRotateRightMultipleElementsList() {
|
||||||
|
RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists();
|
||||||
|
|
||||||
|
// Test case: Rotate a list with multiple elements (Rotate by 2)
|
||||||
|
Node head = new Node(1);
|
||||||
|
head.next = new Node(2);
|
||||||
|
head.next.next = new Node(3);
|
||||||
|
head.next.next.next = new Node(4);
|
||||||
|
head.next.next.next.next = new Node(5);
|
||||||
|
|
||||||
|
Node rotated1 = rotator.rotateRight(head, 2);
|
||||||
|
assertEquals(4, rotated1.value);
|
||||||
|
assertEquals(5, rotated1.next.value);
|
||||||
|
assertEquals(1, rotated1.next.next.value);
|
||||||
|
assertEquals(2, rotated1.next.next.next.value);
|
||||||
|
assertEquals(3, rotated1.next.next.next.next.value);
|
||||||
|
assertNull(rotated1.next.next.next.next.next);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRotateRightFullRotation() {
|
||||||
|
RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists();
|
||||||
|
|
||||||
|
// Test case: Rotate a list with multiple elements (Full rotation)
|
||||||
|
Node head = new Node(1);
|
||||||
|
head.next = new Node(2);
|
||||||
|
head.next.next = new Node(3);
|
||||||
|
head.next.next.next = new Node(4);
|
||||||
|
head.next.next.next.next = new Node(5);
|
||||||
|
|
||||||
|
Node rotated3 = rotator.rotateRight(head, 7);
|
||||||
|
assertEquals(4, rotated3.value);
|
||||||
|
assertEquals(5, rotated3.next.value);
|
||||||
|
assertEquals(1, rotated3.next.next.value);
|
||||||
|
assertEquals(2, rotated3.next.next.next.value);
|
||||||
|
assertEquals(3, rotated3.next.next.next.next.value);
|
||||||
|
assertNull(rotated3.next.next.next.next.next);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user