Skip to content

refactor: introduce SinglyLinkedListNode #6210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* </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>
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}

Expand Down
Loading