Skip to content

Commit 09950d6

Browse files
Add Rotate a Linkedlist (TheAlgorithms#4345)
1 parent a96ad84 commit 09950d6

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.thealgorithms.datastructures.lists;
2+
3+
/**
4+
* Rotate a list
5+
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
6+
*/
7+
8+
public class RotateSinglyLinkedLists {
9+
public Node rotateRight(Node head, int k) {
10+
if (head == null || head.next == null || k == 0) {
11+
return head;
12+
}
13+
14+
Node curr = head;
15+
int len = 1;
16+
while (curr.next != null) {
17+
curr = curr.next;
18+
len++;
19+
}
20+
21+
curr.next = head;
22+
k = k % len;
23+
k = len - k;
24+
while (k > 0) {
25+
curr = curr.next;
26+
k--;
27+
}
28+
29+
head = curr.next;
30+
curr.next = null;
31+
return head;
32+
}
33+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.thealgorithms.datastructures.lists;
2+
3+
/**
4+
* Test cases for RotateSinglyLinkedLists
5+
* Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
6+
*/
7+
8+
import static org.junit.jupiter.api.Assertions.*;
9+
10+
import org.junit.jupiter.api.Test;
11+
12+
public class RotateSinglyLinkedListsTest {
13+
14+
@Test
15+
public void testRotateRightEmptyList() {
16+
RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists();
17+
18+
// Test case: Rotate an empty list
19+
assertNull(rotator.rotateRight(null, 2));
20+
}
21+
22+
@Test
23+
public void testRotateRightSingleNodeList() {
24+
RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists();
25+
26+
// Test case: Rotate a list with one element
27+
Node singleNode = new Node(5);
28+
Node rotatedSingleNode = rotator.rotateRight(singleNode, 3);
29+
assertEquals(5, rotatedSingleNode.value);
30+
assertNull(rotatedSingleNode.next);
31+
}
32+
33+
@Test
34+
public void testRotateRightMultipleElementsList() {
35+
RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists();
36+
37+
// Test case: Rotate a list with multiple elements (Rotate by 2)
38+
Node head = new Node(1);
39+
head.next = new Node(2);
40+
head.next.next = new Node(3);
41+
head.next.next.next = new Node(4);
42+
head.next.next.next.next = new Node(5);
43+
44+
Node rotated1 = rotator.rotateRight(head, 2);
45+
assertEquals(4, rotated1.value);
46+
assertEquals(5, rotated1.next.value);
47+
assertEquals(1, rotated1.next.next.value);
48+
assertEquals(2, rotated1.next.next.next.value);
49+
assertEquals(3, rotated1.next.next.next.next.value);
50+
assertNull(rotated1.next.next.next.next.next);
51+
}
52+
53+
@Test
54+
public void testRotateRightFullRotation() {
55+
RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists();
56+
57+
// Test case: Rotate a list with multiple elements (Full rotation)
58+
Node head = new Node(1);
59+
head.next = new Node(2);
60+
head.next.next = new Node(3);
61+
head.next.next.next = new Node(4);
62+
head.next.next.next.next = new Node(5);
63+
64+
Node rotated3 = rotator.rotateRight(head, 7);
65+
assertEquals(4, rotated3.value);
66+
assertEquals(5, rotated3.next.value);
67+
assertEquals(1, rotated3.next.next.value);
68+
assertEquals(2, rotated3.next.next.next.value);
69+
assertEquals(3, rotated3.next.next.next.next.value);
70+
assertNull(rotated3.next.next.next.next.next);
71+
}
72+
}

0 commit comments

Comments
 (0)