mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-05 00:14:33 +08:00
refactor: introduce SinglyLinkedListNode
(#6210)
This commit is contained in:
@ -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);
|
||||
}
|
||||
|
||||
|
@ -42,12 +42,12 @@ public class MergeSortedSinglyLinkedList extends SinglyLinkedList {
|
||||
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;
|
||||
|
@ -105,7 +105,7 @@ package com.thealgorithms.datastructures.lists;
|
||||
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 class QuickSortLinkedList {
|
||||
* @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 @@ public class QuickSortLinkedList {
|
||||
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;
|
||||
}
|
||||
|
@ -14,10 +14,10 @@ package com.thealgorithms.datastructures.lists;
|
||||
* </p>
|
||||
* <p>
|
||||
* 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.
|
||||
* </p>
|
||||
* <p>
|
||||
@ -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 class ReverseKGroup {
|
||||
* @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 class ReverseKGroup {
|
||||
* @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);
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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)));
|
||||
}
|
||||
|
||||
|
@ -12,7 +12,7 @@ public class SinglyLinkedList implements Iterable<Integer> {
|
||||
/**
|
||||
* Head refer to the front of the list
|
||||
*/
|
||||
private Node head;
|
||||
private SinglyLinkedListNode head;
|
||||
|
||||
/**
|
||||
* Size of SinglyLinkedList
|
||||
@ -33,7 +33,7 @@ public class SinglyLinkedList implements Iterable<Integer> {
|
||||
* @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 class SinglyLinkedList implements Iterable<Integer> {
|
||||
*
|
||||
*/
|
||||
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 class SinglyLinkedList implements Iterable<Integer> {
|
||||
* 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 class SinglyLinkedList implements Iterable<Integer> {
|
||||
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 class SinglyLinkedList implements Iterable<Integer> {
|
||||
}
|
||||
// Swap next pointer
|
||||
|
||||
Node temp = currentA.next;
|
||||
var temp = currentA.next;
|
||||
currentA.next = currentB.next;
|
||||
currentB.next = temp;
|
||||
}
|
||||
@ -126,12 +126,12 @@ public class SinglyLinkedList implements Iterable<Integer> {
|
||||
* 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 class SinglyLinkedList implements Iterable<Integer> {
|
||||
* 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 class SinglyLinkedList implements Iterable<Integer> {
|
||||
* 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 class SinglyLinkedList implements Iterable<Integer> {
|
||||
*
|
||||
* @return head of the list.
|
||||
*/
|
||||
public Node getHead() {
|
||||
public SinglyLinkedListNode getHead() {
|
||||
return head;
|
||||
}
|
||||
|
||||
@ -206,7 +206,7 @@ public class SinglyLinkedList implements Iterable<Integer> {
|
||||
* Set head of the list.
|
||||
*
|
||||
*/
|
||||
public void setHead(Node head) {
|
||||
public void setHead(SinglyLinkedListNode head) {
|
||||
this.head = head;
|
||||
}
|
||||
|
||||
@ -249,10 +249,10 @@ public class SinglyLinkedList implements Iterable<Integer> {
|
||||
}
|
||||
|
||||
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 class SinglyLinkedList implements Iterable<Integer> {
|
||||
}
|
||||
|
||||
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 class SinglyLinkedList implements Iterable<Integer> {
|
||||
*/
|
||||
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 class SinglyLinkedList implements Iterable<Integer> {
|
||||
return;
|
||||
}
|
||||
|
||||
Node cur = head;
|
||||
SinglyLinkedListNode cur = head;
|
||||
for (int i = 0; i < position - 1; ++i) {
|
||||
cur = cur.next;
|
||||
}
|
||||
@ -359,7 +359,7 @@ public class SinglyLinkedList implements Iterable<Integer> {
|
||||
size--;
|
||||
return;
|
||||
}
|
||||
Node cur = head;
|
||||
SinglyLinkedListNode cur = head;
|
||||
for (int i = 0; i < position - 1; ++i) {
|
||||
cur = cur.next;
|
||||
}
|
||||
@ -376,7 +376,7 @@ public class SinglyLinkedList implements Iterable<Integer> {
|
||||
*/
|
||||
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 class SinglyLinkedList implements Iterable<Integer> {
|
||||
}
|
||||
|
||||
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 class SinglyLinkedList implements Iterable<Integer> {
|
||||
}
|
||||
|
||||
private class SinglyLinkedListIterator implements Iterator<Integer> {
|
||||
private Node current;
|
||||
private SinglyLinkedListNode current;
|
||||
|
||||
SinglyLinkedListIterator() {
|
||||
current = head;
|
||||
@ -474,43 +474,3 @@ public class SinglyLinkedList implements Iterable<Integer> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -20,8 +20,8 @@ public class ReverseKGroupTest {
|
||||
@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 class ReverseKGroupTest {
|
||||
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 class ReverseKGroupTest {
|
||||
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);
|
||||
|
@ -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 class RotateSinglyLinkedListsTest {
|
||||
@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));
|
||||
}
|
||||
}
|
||||
|
@ -18,9 +18,9 @@ public class SinglyLinkedListTest {
|
||||
* @return linked list with pre-defined number of nodes
|
||||
*/
|
||||
private SinglyLinkedList createSampleList(int length) {
|
||||
List<Node> nodeList = new ArrayList<>();
|
||||
List<SinglyLinkedListNode> 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 @@ public class SinglyLinkedListTest {
|
||||
@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 @@ public class SinglyLinkedListTest {
|
||||
|
||||
// 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 @@ public class SinglyLinkedListTest {
|
||||
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 @@ public class SinglyLinkedListTest {
|
||||
|
||||
// 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 @@ public class SinglyLinkedListTest {
|
||||
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 @@ public class SinglyLinkedListTest {
|
||||
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 @@ public class SinglyLinkedListTest {
|
||||
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;
|
||||
|
Reference in New Issue
Block a user