Skip to content

Commit 251e9e1

Browse files
authored
refactor: introduce SinglyLinkedListNode (#6210)
1 parent 22098c7 commit 251e9e1

11 files changed

+149
-155
lines changed

Diff for: src/main/java/com/thealgorithms/datastructures/lists/CountSinglyLinkedListRecursion.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class CountSinglyLinkedListRecursion extends SinglyLinkedList {
1212
* @param head the head node of the list segment being counted.
1313
* @return the count of nodes from the given head node onward.
1414
*/
15-
private int countRecursion(Node head) {
15+
private int countRecursion(SinglyLinkedListNode head) {
1616
return head == null ? 0 : 1 + countRecursion(head.next);
1717
}
1818

Diff for: src/main/java/com/thealgorithms/datastructures/lists/MergeSortedSinglyLinkedList.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ public static SinglyLinkedList merge(SinglyLinkedList listA, SinglyLinkedList li
4242
throw new NullPointerException("Input lists must not be null.");
4343
}
4444

45-
Node headA = listA.getHead();
46-
Node headB = listB.getHead();
45+
SinglyLinkedListNode headA = listA.getHead();
46+
SinglyLinkedListNode headB = listB.getHead();
4747
int size = listA.size() + listB.size();
4848

49-
Node head = new Node();
50-
Node tail = head;
49+
SinglyLinkedListNode head = new SinglyLinkedListNode();
50+
SinglyLinkedListNode tail = head;
5151
while (headA != null && headB != null) {
5252
if (headA.value <= headB.value) {
5353
tail.next = headA;

Diff for: src/main/java/com/thealgorithms/datastructures/lists/QuickSortLinkedList.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
public class QuickSortLinkedList {
106106

107107
private final SinglyLinkedList list; // The linked list to be sorted
108-
private Node head; // Head of the list
108+
private SinglyLinkedListNode head; // Head of the list
109109

110110
/**
111111
* Constructor that initializes the QuickSortLinkedList with a given linked list.
@@ -136,19 +136,19 @@ public void sortList() {
136136
* @param head The head node of the list to sort
137137
* @return The head node of the sorted linked list
138138
*/
139-
private Node sortList(Node head) {
139+
private SinglyLinkedListNode sortList(SinglyLinkedListNode head) {
140140
if (head == null || head.next == null) {
141141
return head;
142142
}
143143

144-
Node pivot = head;
144+
SinglyLinkedListNode pivot = head;
145145
head = head.next;
146146
pivot.next = null;
147147

148-
Node lessHead = new Node();
149-
Node lessTail = lessHead;
150-
Node greaterHead = new Node();
151-
Node greaterTail = greaterHead;
148+
SinglyLinkedListNode lessHead = new SinglyLinkedListNode();
149+
SinglyLinkedListNode lessTail = lessHead;
150+
SinglyLinkedListNode greaterHead = new SinglyLinkedListNode();
151+
SinglyLinkedListNode greaterTail = greaterHead;
152152

153153
while (head != null) {
154154
if (head.value < pivot.value) {
@@ -164,14 +164,14 @@ private Node sortList(Node head) {
164164
lessTail.next = null;
165165
greaterTail.next = null;
166166

167-
Node sortedLess = sortList(lessHead.next);
168-
Node sortedGreater = sortList(greaterHead.next);
167+
SinglyLinkedListNode sortedLess = sortList(lessHead.next);
168+
SinglyLinkedListNode sortedGreater = sortList(greaterHead.next);
169169

170170
if (sortedLess == null) {
171171
pivot.next = sortedGreater;
172172
return pivot;
173173
} else {
174-
Node current = sortedLess;
174+
SinglyLinkedListNode current = sortedLess;
175175
while (current.next != null) {
176176
current = current.next;
177177
}

Diff for: src/main/java/com/thealgorithms/datastructures/lists/ReverseKGroup.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
* </p>
1515
* <p>
1616
* The implementation contains:
17-
* - {@code length(Node head)}: A method to calculate the length of the linked list.
18-
* - {@code reverse(Node head, int count, int k)}: A helper method that reverses the nodes
17+
* - {@code length(SinglyLinkedListNode head)}: A method to calculate the length of the linked list.
18+
* - {@code reverse(SinglyLinkedListNode head, int count, int k)}: A helper method that reverses the nodes
1919
* in the linked list in groups of k.
20-
* - {@code reverseKGroup(Node head, int k)}: The main method that initiates the reversal
20+
* - {@code reverseKGroup(SinglyLinkedListNode head, int k)}: The main method that initiates the reversal
2121
* process by calling the reverse method.
2222
* </p>
2323
* <p>
@@ -38,8 +38,8 @@ public class ReverseKGroup {
3838
* @param head The head node of the linked list.
3939
* @return The total number of nodes in the linked list.
4040
*/
41-
public int length(Node head) {
42-
Node curr = head;
41+
public int length(SinglyLinkedListNode head) {
42+
SinglyLinkedListNode curr = head;
4343
int count = 0;
4444
while (curr != null) {
4545
curr = curr.next;
@@ -56,14 +56,14 @@ public int length(Node head) {
5656
* @param k The size of the group to reverse.
5757
* @return The new head of the reversed linked list segment.
5858
*/
59-
public Node reverse(Node head, int count, int k) {
59+
public SinglyLinkedListNode reverse(SinglyLinkedListNode head, int count, int k) {
6060
if (count < k) {
6161
return head;
6262
}
63-
Node prev = null;
63+
SinglyLinkedListNode prev = null;
6464
int count1 = 0;
65-
Node curr = head;
66-
Node next = null;
65+
SinglyLinkedListNode curr = head;
66+
SinglyLinkedListNode next = null;
6767
while (curr != null && count1 < k) {
6868
next = curr.next;
6969
curr.next = prev;
@@ -85,7 +85,7 @@ public Node reverse(Node head, int count, int k) {
8585
* @param k The size of the group to reverse.
8686
* @return The head of the modified linked list after reversal.
8787
*/
88-
public Node reverseKGroup(Node head, int k) {
88+
public SinglyLinkedListNode reverseKGroup(SinglyLinkedListNode head, int k) {
8989
int count = length(head);
9090
return reverse(head, count, k);
9191
}

Diff for: src/main/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedLists.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ public class RotateSinglyLinkedLists {
3838
* @param k The number of positions to rotate the list to the right.
3939
* @return The head of the rotated linked list.
4040
*/
41-
public Node rotateRight(Node head, int k) {
41+
public SinglyLinkedListNode rotateRight(SinglyLinkedListNode head, int k) {
4242
if (head == null || head.next == null || k == 0) {
4343
return head;
4444
}
4545

46-
Node curr = head;
46+
SinglyLinkedListNode curr = head;
4747
int len = 1;
4848
while (curr.next != null) {
4949
curr = curr.next;

Diff for: src/main/java/com/thealgorithms/datastructures/lists/SearchSinglyLinkedListRecursion.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class SearchSinglyLinkedListRecursion extends SinglyLinkedList {
3030
* @param key the integer value to be searched for.
3131
* @return {@code true} if the value `key` is present in the list; otherwise, {@code false}.
3232
*/
33-
private boolean searchRecursion(Node node, int key) {
33+
private boolean searchRecursion(SinglyLinkedListNode node, int key) {
3434
return (node != null && (node.value == key || searchRecursion(node.next, key)));
3535
}
3636

0 commit comments

Comments
 (0)