mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 09:06:51 +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.
|
* @param head the head node of the list segment being counted.
|
||||||
* @return the count of nodes from the given head node onward.
|
* @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);
|
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.");
|
throw new NullPointerException("Input lists must not be null.");
|
||||||
}
|
}
|
||||||
|
|
||||||
Node headA = listA.getHead();
|
SinglyLinkedListNode headA = listA.getHead();
|
||||||
Node headB = listB.getHead();
|
SinglyLinkedListNode headB = listB.getHead();
|
||||||
int size = listA.size() + listB.size();
|
int size = listA.size() + listB.size();
|
||||||
|
|
||||||
Node head = new Node();
|
SinglyLinkedListNode head = new SinglyLinkedListNode();
|
||||||
Node tail = head;
|
SinglyLinkedListNode tail = head;
|
||||||
while (headA != null && headB != null) {
|
while (headA != null && headB != null) {
|
||||||
if (headA.value <= headB.value) {
|
if (headA.value <= headB.value) {
|
||||||
tail.next = headA;
|
tail.next = headA;
|
||||||
|
@ -105,7 +105,7 @@ package com.thealgorithms.datastructures.lists;
|
|||||||
public class QuickSortLinkedList {
|
public class QuickSortLinkedList {
|
||||||
|
|
||||||
private final SinglyLinkedList list; // The linked list to be sorted
|
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.
|
* 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
|
* @param head The head node of the list to sort
|
||||||
* @return The head node of the sorted linked list
|
* @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) {
|
if (head == null || head.next == null) {
|
||||||
return head;
|
return head;
|
||||||
}
|
}
|
||||||
|
|
||||||
Node pivot = head;
|
SinglyLinkedListNode pivot = head;
|
||||||
head = head.next;
|
head = head.next;
|
||||||
pivot.next = null;
|
pivot.next = null;
|
||||||
|
|
||||||
Node lessHead = new Node();
|
SinglyLinkedListNode lessHead = new SinglyLinkedListNode();
|
||||||
Node lessTail = lessHead;
|
SinglyLinkedListNode lessTail = lessHead;
|
||||||
Node greaterHead = new Node();
|
SinglyLinkedListNode greaterHead = new SinglyLinkedListNode();
|
||||||
Node greaterTail = greaterHead;
|
SinglyLinkedListNode greaterTail = greaterHead;
|
||||||
|
|
||||||
while (head != null) {
|
while (head != null) {
|
||||||
if (head.value < pivot.value) {
|
if (head.value < pivot.value) {
|
||||||
@ -164,14 +164,14 @@ public class QuickSortLinkedList {
|
|||||||
lessTail.next = null;
|
lessTail.next = null;
|
||||||
greaterTail.next = null;
|
greaterTail.next = null;
|
||||||
|
|
||||||
Node sortedLess = sortList(lessHead.next);
|
SinglyLinkedListNode sortedLess = sortList(lessHead.next);
|
||||||
Node sortedGreater = sortList(greaterHead.next);
|
SinglyLinkedListNode sortedGreater = sortList(greaterHead.next);
|
||||||
|
|
||||||
if (sortedLess == null) {
|
if (sortedLess == null) {
|
||||||
pivot.next = sortedGreater;
|
pivot.next = sortedGreater;
|
||||||
return pivot;
|
return pivot;
|
||||||
} else {
|
} else {
|
||||||
Node current = sortedLess;
|
SinglyLinkedListNode current = sortedLess;
|
||||||
while (current.next != null) {
|
while (current.next != null) {
|
||||||
current = current.next;
|
current = current.next;
|
||||||
}
|
}
|
||||||
|
@ -14,10 +14,10 @@ package com.thealgorithms.datastructures.lists;
|
|||||||
* </p>
|
* </p>
|
||||||
* <p>
|
* <p>
|
||||||
* The implementation contains:
|
* The implementation contains:
|
||||||
* - {@code length(Node head)}: A method to calculate the length of the linked list.
|
* - {@code length(SinglyLinkedListNode 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 reverse(SinglyLinkedListNode head, int count, int k)}: A helper method that reverses the nodes
|
||||||
* in the linked list in groups of k.
|
* 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.
|
* process by calling the reverse method.
|
||||||
* </p>
|
* </p>
|
||||||
* <p>
|
* <p>
|
||||||
@ -38,8 +38,8 @@ public class ReverseKGroup {
|
|||||||
* @param head The head node of the linked list.
|
* @param head The head node of the linked list.
|
||||||
* @return The total number of nodes in the linked list.
|
* @return The total number of nodes in the linked list.
|
||||||
*/
|
*/
|
||||||
public int length(Node head) {
|
public int length(SinglyLinkedListNode head) {
|
||||||
Node curr = head;
|
SinglyLinkedListNode curr = head;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
while (curr != null) {
|
while (curr != null) {
|
||||||
curr = curr.next;
|
curr = curr.next;
|
||||||
@ -56,14 +56,14 @@ public class ReverseKGroup {
|
|||||||
* @param k The size of the group to reverse.
|
* @param k The size of the group to reverse.
|
||||||
* @return The new head of the reversed linked list segment.
|
* @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) {
|
if (count < k) {
|
||||||
return head;
|
return head;
|
||||||
}
|
}
|
||||||
Node prev = null;
|
SinglyLinkedListNode prev = null;
|
||||||
int count1 = 0;
|
int count1 = 0;
|
||||||
Node curr = head;
|
SinglyLinkedListNode curr = head;
|
||||||
Node next = null;
|
SinglyLinkedListNode next = null;
|
||||||
while (curr != null && count1 < k) {
|
while (curr != null && count1 < k) {
|
||||||
next = curr.next;
|
next = curr.next;
|
||||||
curr.next = prev;
|
curr.next = prev;
|
||||||
@ -85,7 +85,7 @@ public class ReverseKGroup {
|
|||||||
* @param k The size of the group to reverse.
|
* @param k The size of the group to reverse.
|
||||||
* @return The head of the modified linked list after reversal.
|
* @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);
|
int count = length(head);
|
||||||
return reverse(head, count, k);
|
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.
|
* @param k The number of positions to rotate the list to the right.
|
||||||
* @return The head of the rotated linked list.
|
* @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) {
|
if (head == null || head.next == null || k == 0) {
|
||||||
return head;
|
return head;
|
||||||
}
|
}
|
||||||
|
|
||||||
Node curr = head;
|
SinglyLinkedListNode curr = head;
|
||||||
int len = 1;
|
int len = 1;
|
||||||
while (curr.next != null) {
|
while (curr.next != null) {
|
||||||
curr = curr.next;
|
curr = curr.next;
|
||||||
|
@ -30,7 +30,7 @@ public class SearchSinglyLinkedListRecursion extends SinglyLinkedList {
|
|||||||
* @param key the integer value to be searched for.
|
* @param key the integer value to be searched for.
|
||||||
* @return {@code true} if the value `key` is present in the list; otherwise, {@code false}.
|
* @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)));
|
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
|
* Head refer to the front of the list
|
||||||
*/
|
*/
|
||||||
private Node head;
|
private SinglyLinkedListNode head;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Size of SinglyLinkedList
|
* Size of SinglyLinkedList
|
||||||
@ -33,7 +33,7 @@ public class SinglyLinkedList implements Iterable<Integer> {
|
|||||||
* @param head the head node of list
|
* @param head the head node of list
|
||||||
* @param size the size of list
|
* @param size the size of list
|
||||||
*/
|
*/
|
||||||
public SinglyLinkedList(Node head, int size) {
|
public SinglyLinkedList(SinglyLinkedListNode head, int size) {
|
||||||
this.head = head;
|
this.head = head;
|
||||||
this.size = size;
|
this.size = size;
|
||||||
}
|
}
|
||||||
@ -44,8 +44,8 @@ public class SinglyLinkedList implements Iterable<Integer> {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public boolean detectLoop() {
|
public boolean detectLoop() {
|
||||||
Node currentNodeFast = head;
|
SinglyLinkedListNode currentNodeFast = head;
|
||||||
Node currentNodeSlow = head;
|
SinglyLinkedListNode currentNodeSlow = head;
|
||||||
while (currentNodeFast != null && currentNodeFast.next != null) {
|
while (currentNodeFast != null && currentNodeFast.next != null) {
|
||||||
currentNodeFast = currentNodeFast.next.next;
|
currentNodeFast = currentNodeFast.next.next;
|
||||||
currentNodeSlow = currentNodeSlow.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
|
* If the length of the list is even then return item number length/2
|
||||||
* @return middle node of the list
|
* @return middle node of the list
|
||||||
*/
|
*/
|
||||||
public Node middle() {
|
public SinglyLinkedListNode middle() {
|
||||||
if (head == null) {
|
if (head == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
Node firstCounter = head;
|
SinglyLinkedListNode firstCounter = head;
|
||||||
Node secondCounter = firstCounter.next;
|
SinglyLinkedListNode secondCounter = firstCounter.next;
|
||||||
while (secondCounter != null && secondCounter.next != null) {
|
while (secondCounter != null && secondCounter.next != null) {
|
||||||
firstCounter = firstCounter.next;
|
firstCounter = firstCounter.next;
|
||||||
secondCounter = secondCounter.next.next;
|
secondCounter = secondCounter.next.next;
|
||||||
@ -82,15 +82,15 @@ public class SinglyLinkedList implements Iterable<Integer> {
|
|||||||
if (valueFirst == valueSecond) {
|
if (valueFirst == valueSecond) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Node previousA = null;
|
SinglyLinkedListNode previousA = null;
|
||||||
Node currentA = head;
|
SinglyLinkedListNode currentA = head;
|
||||||
while (currentA != null && currentA.value != valueFirst) {
|
while (currentA != null && currentA.value != valueFirst) {
|
||||||
previousA = currentA;
|
previousA = currentA;
|
||||||
currentA = currentA.next;
|
currentA = currentA.next;
|
||||||
}
|
}
|
||||||
|
|
||||||
Node previousB = null;
|
SinglyLinkedListNode previousB = null;
|
||||||
Node currentB = head;
|
SinglyLinkedListNode currentB = head;
|
||||||
while (currentB != null && currentB.value != valueSecond) {
|
while (currentB != null && currentB.value != valueSecond) {
|
||||||
previousB = currentB;
|
previousB = currentB;
|
||||||
currentB = currentB.next;
|
currentB = currentB.next;
|
||||||
@ -117,7 +117,7 @@ public class SinglyLinkedList implements Iterable<Integer> {
|
|||||||
}
|
}
|
||||||
// Swap next pointer
|
// Swap next pointer
|
||||||
|
|
||||||
Node temp = currentA.next;
|
var temp = currentA.next;
|
||||||
currentA.next = currentB.next;
|
currentA.next = currentB.next;
|
||||||
currentB.next = temp;
|
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
|
* Reverse a singly linked list[Iterative] from a given node till the end
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public Node reverseListIter(Node node) {
|
public SinglyLinkedListNode reverseListIter(SinglyLinkedListNode node) {
|
||||||
Node prev = null;
|
SinglyLinkedListNode prev = null;
|
||||||
Node curr = node;
|
SinglyLinkedListNode curr = node;
|
||||||
|
|
||||||
while (curr != null && curr.next != null) {
|
while (curr != null && curr.next != null) {
|
||||||
Node next = curr.next;
|
var next = curr.next;
|
||||||
curr.next = prev;
|
curr.next = prev;
|
||||||
prev = curr;
|
prev = curr;
|
||||||
curr = next;
|
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
|
* 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) {
|
if (head == null || head.next == null) {
|
||||||
return head;
|
return head;
|
||||||
}
|
}
|
||||||
|
|
||||||
Node prev = null;
|
SinglyLinkedListNode prev = null;
|
||||||
Node h2 = reverseListRec(head.next);
|
SinglyLinkedListNode h2 = reverseListRec(head.next);
|
||||||
|
|
||||||
head.next.next = head;
|
head.next.next = head;
|
||||||
head.next = prev;
|
head.next = prev;
|
||||||
@ -167,7 +167,7 @@ public class SinglyLinkedList implements Iterable<Integer> {
|
|||||||
* Clear all nodes in the list
|
* Clear all nodes in the list
|
||||||
*/
|
*/
|
||||||
public void clear() {
|
public void clear() {
|
||||||
Node cur = head;
|
SinglyLinkedListNode cur = head;
|
||||||
while (cur != null) {
|
while (cur != null) {
|
||||||
cur = cur.next;
|
cur = cur.next;
|
||||||
}
|
}
|
||||||
@ -198,7 +198,7 @@ public class SinglyLinkedList implements Iterable<Integer> {
|
|||||||
*
|
*
|
||||||
* @return head of the list.
|
* @return head of the list.
|
||||||
*/
|
*/
|
||||||
public Node getHead() {
|
public SinglyLinkedListNode getHead() {
|
||||||
return head;
|
return head;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -206,7 +206,7 @@ public class SinglyLinkedList implements Iterable<Integer> {
|
|||||||
* Set head of the list.
|
* Set head of the list.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public void setHead(Node head) {
|
public void setHead(SinglyLinkedListNode head) {
|
||||||
this.head = head;
|
this.head = head;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -249,10 +249,10 @@ public class SinglyLinkedList implements Iterable<Integer> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void deleteDuplicates() {
|
public void deleteDuplicates() {
|
||||||
Node pred = head;
|
SinglyLinkedListNode pred = head;
|
||||||
// predecessor = the node
|
// predecessor = the node
|
||||||
// having sublist of its duplicates
|
// having sublist of its duplicates
|
||||||
Node newHead = head;
|
SinglyLinkedListNode newHead = head;
|
||||||
while (newHead != null) {
|
while (newHead != null) {
|
||||||
// if it's a beginning of duplicates sublist
|
// if it's a beginning of duplicates sublist
|
||||||
// skip all duplicates
|
// skip all duplicates
|
||||||
@ -273,7 +273,7 @@ public class SinglyLinkedList implements Iterable<Integer> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void print() {
|
public void print() {
|
||||||
Node temp = head;
|
SinglyLinkedListNode temp = head;
|
||||||
while (temp != null && temp.next != null) {
|
while (temp != null && temp.next != null) {
|
||||||
System.out.print(temp.value + "->");
|
System.out.print(temp.value + "->");
|
||||||
temp = temp.next;
|
temp = temp.next;
|
||||||
@ -310,7 +310,7 @@ public class SinglyLinkedList implements Iterable<Integer> {
|
|||||||
*/
|
*/
|
||||||
public void insertNth(int data, int position) {
|
public void insertNth(int data, int position) {
|
||||||
checkBounds(position, 0, size);
|
checkBounds(position, 0, size);
|
||||||
Node newNode = new Node(data);
|
SinglyLinkedListNode newNode = new SinglyLinkedListNode(data);
|
||||||
if (head == null) {
|
if (head == null) {
|
||||||
/* the list is empty */
|
/* the list is empty */
|
||||||
head = newNode;
|
head = newNode;
|
||||||
@ -325,7 +325,7 @@ public class SinglyLinkedList implements Iterable<Integer> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Node cur = head;
|
SinglyLinkedListNode cur = head;
|
||||||
for (int i = 0; i < position - 1; ++i) {
|
for (int i = 0; i < position - 1; ++i) {
|
||||||
cur = cur.next;
|
cur = cur.next;
|
||||||
}
|
}
|
||||||
@ -359,7 +359,7 @@ public class SinglyLinkedList implements Iterable<Integer> {
|
|||||||
size--;
|
size--;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Node cur = head;
|
SinglyLinkedListNode cur = head;
|
||||||
for (int i = 0; i < position - 1; ++i) {
|
for (int i = 0; i < position - 1; ++i) {
|
||||||
cur = cur.next;
|
cur = cur.next;
|
||||||
}
|
}
|
||||||
@ -376,7 +376,7 @@ public class SinglyLinkedList implements Iterable<Integer> {
|
|||||||
*/
|
*/
|
||||||
public int getNth(int index) {
|
public int getNth(int index) {
|
||||||
checkBounds(index, 0, size - 1);
|
checkBounds(index, 0, size - 1);
|
||||||
Node cur = head;
|
SinglyLinkedListNode cur = head;
|
||||||
for (int i = 0; i < index; ++i) {
|
for (int i = 0; i < index; ++i) {
|
||||||
cur = cur.next;
|
cur = cur.next;
|
||||||
}
|
}
|
||||||
@ -440,7 +440,7 @@ public class SinglyLinkedList implements Iterable<Integer> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SinglyLinkedList instance = new SinglyLinkedList();
|
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.setHead(head);
|
||||||
instance.deleteDuplicates();
|
instance.deleteDuplicates();
|
||||||
instance.print();
|
instance.print();
|
||||||
@ -452,7 +452,7 @@ public class SinglyLinkedList implements Iterable<Integer> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private class SinglyLinkedListIterator implements Iterator<Integer> {
|
private class SinglyLinkedListIterator implements Iterator<Integer> {
|
||||||
private Node current;
|
private SinglyLinkedListNode current;
|
||||||
|
|
||||||
SinglyLinkedListIterator() {
|
SinglyLinkedListIterator() {
|
||||||
current = head;
|
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
|
@Test
|
||||||
public void testReverseKGroupWithSingleNodeList() {
|
public void testReverseKGroupWithSingleNodeList() {
|
||||||
ReverseKGroup reverser = new ReverseKGroup();
|
ReverseKGroup reverser = new ReverseKGroup();
|
||||||
Node singleNode = new Node(5);
|
SinglyLinkedListNode singleNode = new SinglyLinkedListNode(5);
|
||||||
Node result = reverser.reverseKGroup(singleNode, 2);
|
SinglyLinkedListNode result = reverser.reverseKGroup(singleNode, 2);
|
||||||
assertEquals(5, result.value);
|
assertEquals(5, result.value);
|
||||||
assertNull(result.next);
|
assertNull(result.next);
|
||||||
}
|
}
|
||||||
@ -31,15 +31,15 @@ public class ReverseKGroupTest {
|
|||||||
ReverseKGroup reverser = new ReverseKGroup();
|
ReverseKGroup reverser = new ReverseKGroup();
|
||||||
|
|
||||||
// Create a list with multiple elements (1 -> 2 -> 3 -> 4 -> 5)
|
// Create a list with multiple elements (1 -> 2 -> 3 -> 4 -> 5)
|
||||||
Node head;
|
SinglyLinkedListNode head;
|
||||||
head = new Node(1);
|
head = new SinglyLinkedListNode(1);
|
||||||
head.next = new Node(2);
|
head.next = new SinglyLinkedListNode(2);
|
||||||
head.next.next = new Node(3);
|
head.next.next = new SinglyLinkedListNode(3);
|
||||||
head.next.next.next = new Node(4);
|
head.next.next.next = new SinglyLinkedListNode(4);
|
||||||
head.next.next.next.next = new Node(5);
|
head.next.next.next.next = new SinglyLinkedListNode(5);
|
||||||
|
|
||||||
// Test reverse with k=2
|
// Test reverse with k=2
|
||||||
Node result1 = reverser.reverseKGroup(head, 2);
|
SinglyLinkedListNode result1 = reverser.reverseKGroup(head, 2);
|
||||||
assertEquals(2, result1.value);
|
assertEquals(2, result1.value);
|
||||||
assertEquals(1, result1.next.value);
|
assertEquals(1, result1.next.value);
|
||||||
assertEquals(4, result1.next.next.value);
|
assertEquals(4, result1.next.next.value);
|
||||||
@ -53,15 +53,15 @@ public class ReverseKGroupTest {
|
|||||||
ReverseKGroup reverser = new ReverseKGroup();
|
ReverseKGroup reverser = new ReverseKGroup();
|
||||||
|
|
||||||
// Create a list with multiple elements (1 -> 2 -> 3 -> 4 -> 5)
|
// Create a list with multiple elements (1 -> 2 -> 3 -> 4 -> 5)
|
||||||
Node head;
|
SinglyLinkedListNode head;
|
||||||
head = new Node(1);
|
head = new SinglyLinkedListNode(1);
|
||||||
head.next = new Node(2);
|
head.next = new SinglyLinkedListNode(2);
|
||||||
head.next.next = new Node(3);
|
head.next.next = new SinglyLinkedListNode(3);
|
||||||
head.next.next.next = new Node(4);
|
head.next.next.next = new SinglyLinkedListNode(4);
|
||||||
head.next.next.next.next = new Node(5);
|
head.next.next.next.next = new SinglyLinkedListNode(5);
|
||||||
|
|
||||||
// Test reverse with k=3
|
// Test reverse with k=3
|
||||||
Node result = reverser.reverseKGroup(head, 3);
|
SinglyLinkedListNode result = reverser.reverseKGroup(head, 3);
|
||||||
assertEquals(3, result.value);
|
assertEquals(3, result.value);
|
||||||
assertEquals(2, result.next.value);
|
assertEquals(2, result.next.value);
|
||||||
assertEquals(1, result.next.next.value);
|
assertEquals(1, result.next.next.value);
|
||||||
|
@ -14,24 +14,24 @@ public class RotateSinglyLinkedListsTest {
|
|||||||
private final RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists();
|
private final RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists();
|
||||||
|
|
||||||
// Helper method to create a linked list from an array of values
|
// 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) {
|
if (values.length == 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Node head = new Node(values[0]);
|
SinglyLinkedListNode head = new SinglyLinkedListNode(values[0]);
|
||||||
Node current = head;
|
SinglyLinkedListNode current = head;
|
||||||
for (int i = 1; i < values.length; i++) {
|
for (int i = 1; i < values.length; i++) {
|
||||||
current.next = new Node(values[i]);
|
current.next = new SinglyLinkedListNode(values[i]);
|
||||||
current = current.next;
|
current = current.next;
|
||||||
}
|
}
|
||||||
return head;
|
return head;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper method to convert a linked list to a string for easy comparison
|
// 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();
|
StringBuilder sb = new StringBuilder();
|
||||||
Node current = head;
|
SinglyLinkedListNode current = head;
|
||||||
while (current != null) {
|
while (current != null) {
|
||||||
sb.append(current.value);
|
sb.append(current.value);
|
||||||
if (current.next != null) {
|
if (current.next != null) {
|
||||||
@ -51,55 +51,55 @@ public class RotateSinglyLinkedListsTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testRotateRightSingleNodeList() {
|
public void testRotateRightSingleNodeList() {
|
||||||
// Rotate a list with a single element
|
// Rotate a list with a single element
|
||||||
Node singleNode = new Node(5);
|
SinglyLinkedListNode singleNode = new SinglyLinkedListNode(5);
|
||||||
Node rotatedSingleNode = rotator.rotateRight(singleNode, 3);
|
SinglyLinkedListNode rotatedSingleNode = rotator.rotateRight(singleNode, 3);
|
||||||
assertEquals("5", linkedListToString(rotatedSingleNode));
|
assertEquals("5", linkedListToString(rotatedSingleNode));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRotateRightMultipleElementsList() {
|
public void testRotateRightMultipleElementsList() {
|
||||||
// Rotate a list with multiple elements (rotate by 2)
|
// Rotate a list with multiple elements (rotate by 2)
|
||||||
Node head = createLinkedList(new int[] {1, 2, 3, 4, 5});
|
SinglyLinkedListNode head = createLinkedList(new int[] {1, 2, 3, 4, 5});
|
||||||
Node rotated = rotator.rotateRight(head, 2);
|
SinglyLinkedListNode rotated = rotator.rotateRight(head, 2);
|
||||||
assertEquals("4 -> 5 -> 1 -> 2 -> 3", linkedListToString(rotated));
|
assertEquals("4 -> 5 -> 1 -> 2 -> 3", linkedListToString(rotated));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRotateRightFullRotation() {
|
public void testRotateRightFullRotation() {
|
||||||
// Rotate by more than the length of the list
|
// Rotate by more than the length of the list
|
||||||
Node head = createLinkedList(new int[] {1, 2, 3, 4, 5});
|
SinglyLinkedListNode head = createLinkedList(new int[] {1, 2, 3, 4, 5});
|
||||||
Node rotated = rotator.rotateRight(head, 7);
|
SinglyLinkedListNode rotated = rotator.rotateRight(head, 7);
|
||||||
assertEquals("4 -> 5 -> 1 -> 2 -> 3", linkedListToString(rotated));
|
assertEquals("4 -> 5 -> 1 -> 2 -> 3", linkedListToString(rotated));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRotateRightZeroRotation() {
|
public void testRotateRightZeroRotation() {
|
||||||
// Rotate a list by k = 0 (no rotation)
|
// Rotate a list by k = 0 (no rotation)
|
||||||
Node head = createLinkedList(new int[] {1, 2, 3, 4, 5});
|
SinglyLinkedListNode head = createLinkedList(new int[] {1, 2, 3, 4, 5});
|
||||||
Node rotated = rotator.rotateRight(head, 0);
|
SinglyLinkedListNode rotated = rotator.rotateRight(head, 0);
|
||||||
assertEquals("1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated));
|
assertEquals("1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRotateRightByListLength() {
|
public void testRotateRightByListLength() {
|
||||||
// Rotate a list by k equal to list length (no change)
|
// Rotate a list by k equal to list length (no change)
|
||||||
Node head = createLinkedList(new int[] {1, 2, 3, 4, 5});
|
SinglyLinkedListNode head = createLinkedList(new int[] {1, 2, 3, 4, 5});
|
||||||
Node rotated = rotator.rotateRight(head, 5);
|
SinglyLinkedListNode rotated = rotator.rotateRight(head, 5);
|
||||||
assertEquals("1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated));
|
assertEquals("1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRotateRightByMultipleOfListLength() {
|
public void testRotateRightByMultipleOfListLength() {
|
||||||
Node head = createLinkedList(new int[] {1, 2, 3, 4, 5});
|
SinglyLinkedListNode head = createLinkedList(new int[] {1, 2, 3, 4, 5});
|
||||||
Node rotated = rotator.rotateRight(head, 10); // k = 2 * list length
|
SinglyLinkedListNode rotated = rotator.rotateRight(head, 10); // k = 2 * list length
|
||||||
assertEquals("1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated));
|
assertEquals("1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRotateRightLongerList() {
|
public void testRotateRightLongerList() {
|
||||||
// Rotate a longer list by a smaller k
|
// Rotate a longer list by a smaller k
|
||||||
Node head = createLinkedList(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9});
|
SinglyLinkedListNode head = createLinkedList(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9});
|
||||||
Node rotated = rotator.rotateRight(head, 4);
|
SinglyLinkedListNode rotated = rotator.rotateRight(head, 4);
|
||||||
assertEquals("6 -> 7 -> 8 -> 9 -> 1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated));
|
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
|
* @return linked list with pre-defined number of nodes
|
||||||
*/
|
*/
|
||||||
private SinglyLinkedList createSampleList(int length) {
|
private SinglyLinkedList createSampleList(int length) {
|
||||||
List<Node> nodeList = new ArrayList<>();
|
List<SinglyLinkedListNode> nodeList = new ArrayList<>();
|
||||||
for (int i = 1; i <= length; i++) {
|
for (int i = 1; i <= length; i++) {
|
||||||
Node node = new Node(i);
|
SinglyLinkedListNode node = new SinglyLinkedListNode(i);
|
||||||
nodeList.add(node);
|
nodeList.add(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,10 +34,10 @@ public class SinglyLinkedListTest {
|
|||||||
@Test
|
@Test
|
||||||
void detectLoop() {
|
void detectLoop() {
|
||||||
// List has cycle
|
// List has cycle
|
||||||
Node firstNode = new Node(1);
|
SinglyLinkedListNode firstNode = new SinglyLinkedListNode(1);
|
||||||
Node secondNode = new Node(2);
|
SinglyLinkedListNode secondNode = new SinglyLinkedListNode(2);
|
||||||
Node thirdNode = new Node(3);
|
SinglyLinkedListNode thirdNode = new SinglyLinkedListNode(3);
|
||||||
Node fourthNode = new Node(4);
|
SinglyLinkedListNode fourthNode = new SinglyLinkedListNode(4);
|
||||||
|
|
||||||
firstNode.next = secondNode;
|
firstNode.next = secondNode;
|
||||||
secondNode.next = thirdNode;
|
secondNode.next = thirdNode;
|
||||||
@ -112,13 +112,13 @@ public class SinglyLinkedListTest {
|
|||||||
|
|
||||||
// Reversing the LinkedList using reverseList() method and storing the head of the reversed
|
// 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
|
// 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
|
// Recording the Nodes after reversing the LinkedList
|
||||||
Node firstNode = head; // 4
|
SinglyLinkedListNode firstNode = head; // 4
|
||||||
Node secondNode = firstNode.next; // 3
|
SinglyLinkedListNode secondNode = firstNode.next; // 3
|
||||||
Node thirdNode = secondNode.next; // 2
|
SinglyLinkedListNode thirdNode = secondNode.next; // 2
|
||||||
Node fourthNode = thirdNode.next; // 1
|
SinglyLinkedListNode fourthNode = thirdNode.next; // 1
|
||||||
|
|
||||||
// Checking whether the LinkedList is reversed or not by comparing the original list and
|
// Checking whether the LinkedList is reversed or not by comparing the original list and
|
||||||
// reversed list nodes
|
// reversed list nodes
|
||||||
@ -134,10 +134,10 @@ public class SinglyLinkedListTest {
|
|||||||
void reverseListNullPointer() {
|
void reverseListNullPointer() {
|
||||||
// Creating a linkedlist with first node assigned to null
|
// Creating a linkedlist with first node assigned to null
|
||||||
SinglyLinkedList list = new SinglyLinkedList();
|
SinglyLinkedList list = new SinglyLinkedList();
|
||||||
Node first = list.getHead();
|
SinglyLinkedListNode first = list.getHead();
|
||||||
|
|
||||||
// Reversing the linkedlist
|
// Reversing the linkedlist
|
||||||
Node head = list.reverseListIter(first);
|
SinglyLinkedListNode head = list.reverseListIter(first);
|
||||||
|
|
||||||
// checking whether the method works fine if the input is null
|
// checking whether the method works fine if the input is null
|
||||||
assertEquals(head, first);
|
assertEquals(head, first);
|
||||||
@ -151,10 +151,10 @@ public class SinglyLinkedListTest {
|
|||||||
|
|
||||||
// Reversing the LinkedList using reverseList() method and storing the head of the reversed
|
// Reversing the LinkedList using reverseList() method and storing the head of the reversed
|
||||||
// linkedlist in a head node
|
// 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
|
// 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
|
int i = 20; // This is for the comparison of values of nodes of the reversed linkedlist
|
||||||
// Checking whether the reverseList() method performed its task
|
// Checking whether the reverseList() method performed its task
|
||||||
@ -171,7 +171,7 @@ public class SinglyLinkedListTest {
|
|||||||
SinglyLinkedList list = createSampleList(5);
|
SinglyLinkedList list = createSampleList(5);
|
||||||
|
|
||||||
// Reversing the linked list using reverseList() method
|
// 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
|
// Check if the reversed list is: 5 -> 4 -> 3 -> 2 -> 1
|
||||||
assertEquals(5, head.value);
|
assertEquals(5, head.value);
|
||||||
@ -185,10 +185,10 @@ public class SinglyLinkedListTest {
|
|||||||
void recursiveReverseListNullPointer() {
|
void recursiveReverseListNullPointer() {
|
||||||
// Create an empty linked list
|
// Create an empty linked list
|
||||||
SinglyLinkedList list = new SinglyLinkedList();
|
SinglyLinkedList list = new SinglyLinkedList();
|
||||||
Node first = list.getHead();
|
SinglyLinkedListNode first = list.getHead();
|
||||||
|
|
||||||
// Reversing the empty linked list
|
// Reversing the empty linked list
|
||||||
Node head = list.reverseListRec(first);
|
SinglyLinkedListNode head = list.reverseListRec(first);
|
||||||
|
|
||||||
// Check if the head remains the same (null)
|
// Check if the head remains the same (null)
|
||||||
assertNull(head);
|
assertNull(head);
|
||||||
@ -200,11 +200,11 @@ public class SinglyLinkedListTest {
|
|||||||
SinglyLinkedList list = createSampleList(20);
|
SinglyLinkedList list = createSampleList(20);
|
||||||
|
|
||||||
// Reversing the linked list using reverseList() method
|
// 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
|
// Check if the reversed list has the correct values
|
||||||
int i = 20;
|
int i = 20;
|
||||||
Node temp = head;
|
SinglyLinkedListNode temp = head;
|
||||||
while (temp != null && i > 0) {
|
while (temp != null && i > 0) {
|
||||||
assertEquals(i, temp.value);
|
assertEquals(i, temp.value);
|
||||||
temp = temp.next;
|
temp = temp.next;
|
||||||
|
Reference in New Issue
Block a user