diff --git a/src/main/java/com/thealgorithms/datastructures/lists/CountSinglyLinkedListRecursion.java b/src/main/java/com/thealgorithms/datastructures/lists/CountSinglyLinkedListRecursion.java
index 4c1ffe9d3ea4..b58d51e7e5fe 100644
--- a/src/main/java/com/thealgorithms/datastructures/lists/CountSinglyLinkedListRecursion.java
+++ b/src/main/java/com/thealgorithms/datastructures/lists/CountSinglyLinkedListRecursion.java
@@ -12,7 +12,7 @@ public class CountSinglyLinkedListRecursion extends SinglyLinkedList {
* @param head the head node of the list segment being counted.
* @return the count of nodes from the given head node onward.
*/
- private int countRecursion(Node head) {
+ private int countRecursion(SinglyLinkedListNode head) {
return head == null ? 0 : 1 + countRecursion(head.next);
}
diff --git a/src/main/java/com/thealgorithms/datastructures/lists/MergeSortedSinglyLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/MergeSortedSinglyLinkedList.java
index a16b202c4505..4e99642fccd8 100644
--- a/src/main/java/com/thealgorithms/datastructures/lists/MergeSortedSinglyLinkedList.java
+++ b/src/main/java/com/thealgorithms/datastructures/lists/MergeSortedSinglyLinkedList.java
@@ -42,12 +42,12 @@ public static SinglyLinkedList merge(SinglyLinkedList listA, SinglyLinkedList li
throw new NullPointerException("Input lists must not be null.");
}
- Node headA = listA.getHead();
- Node headB = listB.getHead();
+ SinglyLinkedListNode headA = listA.getHead();
+ SinglyLinkedListNode headB = listB.getHead();
int size = listA.size() + listB.size();
- Node head = new Node();
- Node tail = head;
+ SinglyLinkedListNode head = new SinglyLinkedListNode();
+ SinglyLinkedListNode tail = head;
while (headA != null && headB != null) {
if (headA.value <= headB.value) {
tail.next = headA;
diff --git a/src/main/java/com/thealgorithms/datastructures/lists/QuickSortLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/QuickSortLinkedList.java
index 08fe674b47f4..f018781ada70 100644
--- a/src/main/java/com/thealgorithms/datastructures/lists/QuickSortLinkedList.java
+++ b/src/main/java/com/thealgorithms/datastructures/lists/QuickSortLinkedList.java
@@ -105,7 +105,7 @@
public class QuickSortLinkedList {
private final SinglyLinkedList list; // The linked list to be sorted
- private Node head; // Head of the list
+ private SinglyLinkedListNode head; // Head of the list
/**
* Constructor that initializes the QuickSortLinkedList with a given linked list.
@@ -136,19 +136,19 @@ public void sortList() {
* @param head The head node of the list to sort
* @return The head node of the sorted linked list
*/
- private Node sortList(Node head) {
+ private SinglyLinkedListNode sortList(SinglyLinkedListNode head) {
if (head == null || head.next == null) {
return head;
}
- Node pivot = head;
+ SinglyLinkedListNode pivot = head;
head = head.next;
pivot.next = null;
- Node lessHead = new Node();
- Node lessTail = lessHead;
- Node greaterHead = new Node();
- Node greaterTail = greaterHead;
+ SinglyLinkedListNode lessHead = new SinglyLinkedListNode();
+ SinglyLinkedListNode lessTail = lessHead;
+ SinglyLinkedListNode greaterHead = new SinglyLinkedListNode();
+ SinglyLinkedListNode greaterTail = greaterHead;
while (head != null) {
if (head.value < pivot.value) {
@@ -164,14 +164,14 @@ private Node sortList(Node head) {
lessTail.next = null;
greaterTail.next = null;
- Node sortedLess = sortList(lessHead.next);
- Node sortedGreater = sortList(greaterHead.next);
+ SinglyLinkedListNode sortedLess = sortList(lessHead.next);
+ SinglyLinkedListNode sortedGreater = sortList(greaterHead.next);
if (sortedLess == null) {
pivot.next = sortedGreater;
return pivot;
} else {
- Node current = sortedLess;
+ SinglyLinkedListNode current = sortedLess;
while (current.next != null) {
current = current.next;
}
diff --git a/src/main/java/com/thealgorithms/datastructures/lists/ReverseKGroup.java b/src/main/java/com/thealgorithms/datastructures/lists/ReverseKGroup.java
index c9a5c1df9870..9b9464d388b5 100644
--- a/src/main/java/com/thealgorithms/datastructures/lists/ReverseKGroup.java
+++ b/src/main/java/com/thealgorithms/datastructures/lists/ReverseKGroup.java
@@ -14,10 +14,10 @@
*
*
* The implementation contains:
- * - {@code length(Node head)}: A method to calculate the length of the linked list.
- * - {@code reverse(Node head, int count, int k)}: A helper method that reverses the nodes
+ * - {@code length(SinglyLinkedListNode head)}: A method to calculate the length of the linked list.
+ * - {@code reverse(SinglyLinkedListNode head, int count, int k)}: A helper method that reverses the nodes
* in the linked list in groups of k.
- * - {@code reverseKGroup(Node head, int k)}: The main method that initiates the reversal
+ * - {@code reverseKGroup(SinglyLinkedListNode head, int k)}: The main method that initiates the reversal
* process by calling the reverse method.
*
*
@@ -38,8 +38,8 @@ public class ReverseKGroup {
* @param head The head node of the linked list.
* @return The total number of nodes in the linked list.
*/
- public int length(Node head) {
- Node curr = head;
+ public int length(SinglyLinkedListNode head) {
+ SinglyLinkedListNode curr = head;
int count = 0;
while (curr != null) {
curr = curr.next;
@@ -56,14 +56,14 @@ public int length(Node head) {
* @param k The size of the group to reverse.
* @return The new head of the reversed linked list segment.
*/
- public Node reverse(Node head, int count, int k) {
+ public SinglyLinkedListNode reverse(SinglyLinkedListNode head, int count, int k) {
if (count < k) {
return head;
}
- Node prev = null;
+ SinglyLinkedListNode prev = null;
int count1 = 0;
- Node curr = head;
- Node next = null;
+ SinglyLinkedListNode curr = head;
+ SinglyLinkedListNode next = null;
while (curr != null && count1 < k) {
next = curr.next;
curr.next = prev;
@@ -85,7 +85,7 @@ public Node reverse(Node head, int count, int k) {
* @param k The size of the group to reverse.
* @return The head of the modified linked list after reversal.
*/
- public Node reverseKGroup(Node head, int k) {
+ public SinglyLinkedListNode reverseKGroup(SinglyLinkedListNode head, int k) {
int count = length(head);
return reverse(head, count, k);
}
diff --git a/src/main/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedLists.java b/src/main/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedLists.java
index 7676cc343653..47ee5397097c 100644
--- a/src/main/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedLists.java
+++ b/src/main/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedLists.java
@@ -38,12 +38,12 @@ public class RotateSinglyLinkedLists {
* @param k The number of positions to rotate the list to the right.
* @return The head of the rotated linked list.
*/
- public Node rotateRight(Node head, int k) {
+ public SinglyLinkedListNode rotateRight(SinglyLinkedListNode head, int k) {
if (head == null || head.next == null || k == 0) {
return head;
}
- Node curr = head;
+ SinglyLinkedListNode curr = head;
int len = 1;
while (curr.next != null) {
curr = curr.next;
diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SearchSinglyLinkedListRecursion.java b/src/main/java/com/thealgorithms/datastructures/lists/SearchSinglyLinkedListRecursion.java
index a40e9b2a1a66..4ac2de422595 100644
--- a/src/main/java/com/thealgorithms/datastructures/lists/SearchSinglyLinkedListRecursion.java
+++ b/src/main/java/com/thealgorithms/datastructures/lists/SearchSinglyLinkedListRecursion.java
@@ -30,7 +30,7 @@ public class SearchSinglyLinkedListRecursion extends SinglyLinkedList {
* @param key the integer value to be searched for.
* @return {@code true} if the value `key` is present in the list; otherwise, {@code false}.
*/
- private boolean searchRecursion(Node node, int key) {
+ private boolean searchRecursion(SinglyLinkedListNode node, int key) {
return (node != null && (node.value == key || searchRecursion(node.next, key)));
}
diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java
index eb6cdf48f58b..ff4af4437cc7 100644
--- a/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java
+++ b/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java
@@ -12,7 +12,7 @@ public class SinglyLinkedList implements Iterable {
/**
* Head refer to the front of the list
*/
- private Node head;
+ private SinglyLinkedListNode head;
/**
* Size of SinglyLinkedList
@@ -33,7 +33,7 @@ public SinglyLinkedList() {
* @param head the head node of list
* @param size the size of list
*/
- public SinglyLinkedList(Node head, int size) {
+ public SinglyLinkedList(SinglyLinkedListNode head, int size) {
this.head = head;
this.size = size;
}
@@ -44,8 +44,8 @@ public SinglyLinkedList(Node head, int size) {
*
*/
public boolean detectLoop() {
- Node currentNodeFast = head;
- Node currentNodeSlow = head;
+ SinglyLinkedListNode currentNodeFast = head;
+ SinglyLinkedListNode currentNodeSlow = head;
while (currentNodeFast != null && currentNodeFast.next != null) {
currentNodeFast = currentNodeFast.next.next;
currentNodeSlow = currentNodeSlow.next;
@@ -61,12 +61,12 @@ public boolean detectLoop() {
* If the length of the list is even then return item number length/2
* @return middle node of the list
*/
- public Node middle() {
+ public SinglyLinkedListNode middle() {
if (head == null) {
return null;
}
- Node firstCounter = head;
- Node secondCounter = firstCounter.next;
+ SinglyLinkedListNode firstCounter = head;
+ SinglyLinkedListNode secondCounter = firstCounter.next;
while (secondCounter != null && secondCounter.next != null) {
firstCounter = firstCounter.next;
secondCounter = secondCounter.next.next;
@@ -82,15 +82,15 @@ public void swapNodes(int valueFirst, int valueSecond) {
if (valueFirst == valueSecond) {
return;
}
- Node previousA = null;
- Node currentA = head;
+ SinglyLinkedListNode previousA = null;
+ SinglyLinkedListNode currentA = head;
while (currentA != null && currentA.value != valueFirst) {
previousA = currentA;
currentA = currentA.next;
}
- Node previousB = null;
- Node currentB = head;
+ SinglyLinkedListNode previousB = null;
+ SinglyLinkedListNode currentB = head;
while (currentB != null && currentB.value != valueSecond) {
previousB = currentB;
currentB = currentB.next;
@@ -117,7 +117,7 @@ public void swapNodes(int valueFirst, int valueSecond) {
}
// Swap next pointer
- Node temp = currentA.next;
+ var temp = currentA.next;
currentA.next = currentB.next;
currentB.next = temp;
}
@@ -126,12 +126,12 @@ public void swapNodes(int valueFirst, int valueSecond) {
* Reverse a singly linked list[Iterative] from a given node till the end
*
*/
- public Node reverseListIter(Node node) {
- Node prev = null;
- Node curr = node;
+ public SinglyLinkedListNode reverseListIter(SinglyLinkedListNode node) {
+ SinglyLinkedListNode prev = null;
+ SinglyLinkedListNode curr = node;
while (curr != null && curr.next != null) {
- Node next = curr.next;
+ var next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
@@ -149,13 +149,13 @@ public Node reverseListIter(Node node) {
* Reverse a singly linked list[Recursive] from a given node till the end
*
*/
- public Node reverseListRec(Node head) {
+ public SinglyLinkedListNode reverseListRec(SinglyLinkedListNode head) {
if (head == null || head.next == null) {
return head;
}
- Node prev = null;
- Node h2 = reverseListRec(head.next);
+ SinglyLinkedListNode prev = null;
+ SinglyLinkedListNode h2 = reverseListRec(head.next);
head.next.next = head;
head.next = prev;
@@ -167,7 +167,7 @@ public Node reverseListRec(Node head) {
* Clear all nodes in the list
*/
public void clear() {
- Node cur = head;
+ SinglyLinkedListNode cur = head;
while (cur != null) {
cur = cur.next;
}
@@ -198,7 +198,7 @@ public int size() {
*
* @return head of the list.
*/
- public Node getHead() {
+ public SinglyLinkedListNode getHead() {
return head;
}
@@ -206,7 +206,7 @@ public Node getHead() {
* Set head of the list.
*
*/
- public void setHead(Node head) {
+ public void setHead(SinglyLinkedListNode head) {
this.head = head;
}
@@ -249,10 +249,10 @@ public String toString() {
}
public void deleteDuplicates() {
- Node pred = head;
+ SinglyLinkedListNode pred = head;
// predecessor = the node
// having sublist of its duplicates
- Node newHead = head;
+ SinglyLinkedListNode newHead = head;
while (newHead != null) {
// if it's a beginning of duplicates sublist
// skip all duplicates
@@ -273,7 +273,7 @@ public void deleteDuplicates() {
}
public void print() {
- Node temp = head;
+ SinglyLinkedListNode temp = head;
while (temp != null && temp.next != null) {
System.out.print(temp.value + "->");
temp = temp.next;
@@ -310,7 +310,7 @@ public void insert(int data) {
*/
public void insertNth(int data, int position) {
checkBounds(position, 0, size);
- Node newNode = new Node(data);
+ SinglyLinkedListNode newNode = new SinglyLinkedListNode(data);
if (head == null) {
/* the list is empty */
head = newNode;
@@ -325,7 +325,7 @@ public void insertNth(int data, int position) {
return;
}
- Node cur = head;
+ SinglyLinkedListNode cur = head;
for (int i = 0; i < position - 1; ++i) {
cur = cur.next;
}
@@ -359,7 +359,7 @@ public void deleteNth(int position) {
size--;
return;
}
- Node cur = head;
+ SinglyLinkedListNode cur = head;
for (int i = 0; i < position - 1; ++i) {
cur = cur.next;
}
@@ -376,7 +376,7 @@ public void deleteNth(int position) {
*/
public int getNth(int index) {
checkBounds(index, 0, size - 1);
- Node cur = head;
+ SinglyLinkedListNode cur = head;
for (int i = 0; i < index; ++i) {
cur = cur.next;
}
@@ -440,7 +440,7 @@ public static void main(String[] arg) {
}
SinglyLinkedList instance = new SinglyLinkedList();
- Node head = new Node(0, new Node(2, new Node(3, new Node(3, new Node(4)))));
+ SinglyLinkedListNode head = new SinglyLinkedListNode(0, new SinglyLinkedListNode(2, new SinglyLinkedListNode(3, new SinglyLinkedListNode(3, new SinglyLinkedListNode(4)))));
instance.setHead(head);
instance.deleteDuplicates();
instance.print();
@@ -452,7 +452,7 @@ public Iterator iterator() {
}
private class SinglyLinkedListIterator implements Iterator {
- private Node current;
+ private SinglyLinkedListNode current;
SinglyLinkedListIterator() {
current = head;
@@ -474,43 +474,3 @@ public Integer next() {
}
}
}
-
-/**
- * This class is the nodes of the SinglyLinked List. They consist of a value and
- * a pointer to the node after them.
- */
-class Node {
-
- /**
- * The value of the node
- */
- int value;
-
- /**
- * Point to the next node
- */
- Node next;
-
- Node() {
- }
-
- /**
- * Constructor
- *
- * @param value Value to be put in the node
- */
- Node(int value) {
- this(value, null);
- }
-
- /**
- * Constructor
- *
- * @param value Value to be put in the node
- * @param next Reference to the next node
- */
- Node(int value, Node next) {
- this.value = value;
- this.next = next;
- }
-}
diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedListNode.java b/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedListNode.java
new file mode 100644
index 000000000000..d0a06369215a
--- /dev/null
+++ b/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedListNode.java
@@ -0,0 +1,34 @@
+package com.thealgorithms.datastructures.lists;
+
+/**
+ * This class is the nodes of the SinglyLinked List. They consist of a value and
+ * a pointer to the node after them.
+ */
+class SinglyLinkedListNode {
+
+ int value;
+ SinglyLinkedListNode next = null;
+
+ SinglyLinkedListNode() {
+ }
+
+ /**
+ * Constructor
+ *
+ * @param value Value to be put in the node
+ */
+ SinglyLinkedListNode(int value) {
+ this(value, null);
+ }
+
+ /**
+ * Constructor
+ *
+ * @param value Value to be put in the node
+ * @param next Reference to the next node
+ */
+ SinglyLinkedListNode(int value, SinglyLinkedListNode next) {
+ this.value = value;
+ this.next = next;
+ }
+}
diff --git a/src/test/java/com/thealgorithms/datastructures/lists/ReverseKGroupTest.java b/src/test/java/com/thealgorithms/datastructures/lists/ReverseKGroupTest.java
index b2db478f692c..76b7ab063de4 100644
--- a/src/test/java/com/thealgorithms/datastructures/lists/ReverseKGroupTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/lists/ReverseKGroupTest.java
@@ -20,8 +20,8 @@ public void testReverseKGroupWithEmptyList() {
@Test
public void testReverseKGroupWithSingleNodeList() {
ReverseKGroup reverser = new ReverseKGroup();
- Node singleNode = new Node(5);
- Node result = reverser.reverseKGroup(singleNode, 2);
+ SinglyLinkedListNode singleNode = new SinglyLinkedListNode(5);
+ SinglyLinkedListNode result = reverser.reverseKGroup(singleNode, 2);
assertEquals(5, result.value);
assertNull(result.next);
}
@@ -31,15 +31,15 @@ public void testReverseKGroupWithKEqualTo2() {
ReverseKGroup reverser = new ReverseKGroup();
// Create a list with multiple elements (1 -> 2 -> 3 -> 4 -> 5)
- Node head;
- 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);
+ SinglyLinkedListNode head;
+ head = new SinglyLinkedListNode(1);
+ head.next = new SinglyLinkedListNode(2);
+ head.next.next = new SinglyLinkedListNode(3);
+ head.next.next.next = new SinglyLinkedListNode(4);
+ head.next.next.next.next = new SinglyLinkedListNode(5);
// Test reverse with k=2
- Node result1 = reverser.reverseKGroup(head, 2);
+ SinglyLinkedListNode result1 = reverser.reverseKGroup(head, 2);
assertEquals(2, result1.value);
assertEquals(1, result1.next.value);
assertEquals(4, result1.next.next.value);
@@ -53,15 +53,15 @@ public void testReverseKGroupWithKEqualTo3() {
ReverseKGroup reverser = new ReverseKGroup();
// Create a list with multiple elements (1 -> 2 -> 3 -> 4 -> 5)
- Node head;
- 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);
+ SinglyLinkedListNode head;
+ head = new SinglyLinkedListNode(1);
+ head.next = new SinglyLinkedListNode(2);
+ head.next.next = new SinglyLinkedListNode(3);
+ head.next.next.next = new SinglyLinkedListNode(4);
+ head.next.next.next.next = new SinglyLinkedListNode(5);
// Test reverse with k=3
- Node result = reverser.reverseKGroup(head, 3);
+ SinglyLinkedListNode result = reverser.reverseKGroup(head, 3);
assertEquals(3, result.value);
assertEquals(2, result.next.value);
assertEquals(1, result.next.next.value);
diff --git a/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java b/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java
index 70c0dfccafa4..c476ad1b0203 100644
--- a/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java
@@ -14,24 +14,24 @@ public class RotateSinglyLinkedListsTest {
private final RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists();
// Helper method to create a linked list from an array of values
- private Node createLinkedList(int[] values) {
+ private SinglyLinkedListNode createLinkedList(int[] values) {
if (values.length == 0) {
return null;
}
- Node head = new Node(values[0]);
- Node current = head;
+ SinglyLinkedListNode head = new SinglyLinkedListNode(values[0]);
+ SinglyLinkedListNode current = head;
for (int i = 1; i < values.length; i++) {
- current.next = new Node(values[i]);
+ current.next = new SinglyLinkedListNode(values[i]);
current = current.next;
}
return head;
}
// Helper method to convert a linked list to a string for easy comparison
- private String linkedListToString(Node head) {
+ private String linkedListToString(SinglyLinkedListNode head) {
StringBuilder sb = new StringBuilder();
- Node current = head;
+ SinglyLinkedListNode current = head;
while (current != null) {
sb.append(current.value);
if (current.next != null) {
@@ -51,55 +51,55 @@ public void testRotateRightEmptyList() {
@Test
public void testRotateRightSingleNodeList() {
// Rotate a list with a single element
- Node singleNode = new Node(5);
- Node rotatedSingleNode = rotator.rotateRight(singleNode, 3);
+ SinglyLinkedListNode singleNode = new SinglyLinkedListNode(5);
+ SinglyLinkedListNode rotatedSingleNode = rotator.rotateRight(singleNode, 3);
assertEquals("5", linkedListToString(rotatedSingleNode));
}
@Test
public void testRotateRightMultipleElementsList() {
// Rotate a list with multiple elements (rotate by 2)
- Node head = createLinkedList(new int[] {1, 2, 3, 4, 5});
- Node rotated = rotator.rotateRight(head, 2);
+ SinglyLinkedListNode head = createLinkedList(new int[] {1, 2, 3, 4, 5});
+ SinglyLinkedListNode rotated = rotator.rotateRight(head, 2);
assertEquals("4 -> 5 -> 1 -> 2 -> 3", linkedListToString(rotated));
}
@Test
public void testRotateRightFullRotation() {
// Rotate by more than the length of the list
- Node head = createLinkedList(new int[] {1, 2, 3, 4, 5});
- Node rotated = rotator.rotateRight(head, 7);
+ SinglyLinkedListNode head = createLinkedList(new int[] {1, 2, 3, 4, 5});
+ SinglyLinkedListNode rotated = rotator.rotateRight(head, 7);
assertEquals("4 -> 5 -> 1 -> 2 -> 3", linkedListToString(rotated));
}
@Test
public void testRotateRightZeroRotation() {
// Rotate a list by k = 0 (no rotation)
- Node head = createLinkedList(new int[] {1, 2, 3, 4, 5});
- Node rotated = rotator.rotateRight(head, 0);
+ SinglyLinkedListNode head = createLinkedList(new int[] {1, 2, 3, 4, 5});
+ SinglyLinkedListNode rotated = rotator.rotateRight(head, 0);
assertEquals("1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated));
}
@Test
public void testRotateRightByListLength() {
// Rotate a list by k equal to list length (no change)
- Node head = createLinkedList(new int[] {1, 2, 3, 4, 5});
- Node rotated = rotator.rotateRight(head, 5);
+ SinglyLinkedListNode head = createLinkedList(new int[] {1, 2, 3, 4, 5});
+ SinglyLinkedListNode rotated = rotator.rotateRight(head, 5);
assertEquals("1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated));
}
@Test
public void testRotateRightByMultipleOfListLength() {
- Node head = createLinkedList(new int[] {1, 2, 3, 4, 5});
- Node rotated = rotator.rotateRight(head, 10); // k = 2 * list length
+ SinglyLinkedListNode head = createLinkedList(new int[] {1, 2, 3, 4, 5});
+ SinglyLinkedListNode rotated = rotator.rotateRight(head, 10); // k = 2 * list length
assertEquals("1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated));
}
@Test
public void testRotateRightLongerList() {
// Rotate a longer list by a smaller k
- Node head = createLinkedList(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9});
- Node rotated = rotator.rotateRight(head, 4);
+ SinglyLinkedListNode head = createLinkedList(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9});
+ SinglyLinkedListNode rotated = rotator.rotateRight(head, 4);
assertEquals("6 -> 7 -> 8 -> 9 -> 1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated));
}
}
diff --git a/src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java
index a47434083cdb..f80c6b5055f0 100644
--- a/src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java
@@ -18,9 +18,9 @@ public class SinglyLinkedListTest {
* @return linked list with pre-defined number of nodes
*/
private SinglyLinkedList createSampleList(int length) {
- List nodeList = new ArrayList<>();
+ List nodeList = new ArrayList<>();
for (int i = 1; i <= length; i++) {
- Node node = new Node(i);
+ SinglyLinkedListNode node = new SinglyLinkedListNode(i);
nodeList.add(node);
}
@@ -34,10 +34,10 @@ private SinglyLinkedList createSampleList(int length) {
@Test
void detectLoop() {
// List has cycle
- Node firstNode = new Node(1);
- Node secondNode = new Node(2);
- Node thirdNode = new Node(3);
- Node fourthNode = new Node(4);
+ SinglyLinkedListNode firstNode = new SinglyLinkedListNode(1);
+ SinglyLinkedListNode secondNode = new SinglyLinkedListNode(2);
+ SinglyLinkedListNode thirdNode = new SinglyLinkedListNode(3);
+ SinglyLinkedListNode fourthNode = new SinglyLinkedListNode(4);
firstNode.next = secondNode;
secondNode.next = thirdNode;
@@ -112,13 +112,13 @@ void reverseList() {
// Reversing the LinkedList using reverseList() method and storing the head of the reversed
// linkedlist in a head node The reversed linkedlist will be 4->3->2->1->null
- Node head = list.reverseListIter(list.getHead());
+ SinglyLinkedListNode head = list.reverseListIter(list.getHead());
// Recording the Nodes after reversing the LinkedList
- Node firstNode = head; // 4
- Node secondNode = firstNode.next; // 3
- Node thirdNode = secondNode.next; // 2
- Node fourthNode = thirdNode.next; // 1
+ SinglyLinkedListNode firstNode = head; // 4
+ SinglyLinkedListNode secondNode = firstNode.next; // 3
+ SinglyLinkedListNode thirdNode = secondNode.next; // 2
+ SinglyLinkedListNode fourthNode = thirdNode.next; // 1
// Checking whether the LinkedList is reversed or not by comparing the original list and
// reversed list nodes
@@ -134,10 +134,10 @@ void reverseList() {
void reverseListNullPointer() {
// Creating a linkedlist with first node assigned to null
SinglyLinkedList list = new SinglyLinkedList();
- Node first = list.getHead();
+ SinglyLinkedListNode first = list.getHead();
// Reversing the linkedlist
- Node head = list.reverseListIter(first);
+ SinglyLinkedListNode head = list.reverseListIter(first);
// checking whether the method works fine if the input is null
assertEquals(head, first);
@@ -151,10 +151,10 @@ void reverseListTest() {
// Reversing the LinkedList using reverseList() method and storing the head of the reversed
// linkedlist in a head node
- Node head = list.reverseListIter(list.getHead());
+ SinglyLinkedListNode head = list.reverseListIter(list.getHead());
// Storing the head in a temp variable, so that we cannot loose the track of head
- Node temp = head;
+ SinglyLinkedListNode temp = head;
int i = 20; // This is for the comparison of values of nodes of the reversed linkedlist
// Checking whether the reverseList() method performed its task
@@ -171,7 +171,7 @@ void recursiveReverseList() {
SinglyLinkedList list = createSampleList(5);
// Reversing the linked list using reverseList() method
- Node head = list.reverseListRec(list.getHead());
+ SinglyLinkedListNode head = list.reverseListRec(list.getHead());
// Check if the reversed list is: 5 -> 4 -> 3 -> 2 -> 1
assertEquals(5, head.value);
@@ -185,10 +185,10 @@ void recursiveReverseList() {
void recursiveReverseListNullPointer() {
// Create an empty linked list
SinglyLinkedList list = new SinglyLinkedList();
- Node first = list.getHead();
+ SinglyLinkedListNode first = list.getHead();
// Reversing the empty linked list
- Node head = list.reverseListRec(first);
+ SinglyLinkedListNode head = list.reverseListRec(first);
// Check if the head remains the same (null)
assertNull(head);
@@ -200,11 +200,11 @@ void recursiveReverseListTest() {
SinglyLinkedList list = createSampleList(20);
// Reversing the linked list using reverseList() method
- Node head = list.reverseListRec(list.getHead());
+ SinglyLinkedListNode head = list.reverseListRec(list.getHead());
// Check if the reversed list has the correct values
int i = 20;
- Node temp = head;
+ SinglyLinkedListNode temp = head;
while (temp != null && i > 0) {
assertEquals(i, temp.value);
temp = temp.next;