From 76ee4b491b9cd6b1d7842c9da36dea9c73bbb2c9 Mon Sep 17 00:00:00 2001 From: AYAZ2006 Date: Sun, 5 Oct 2025 18:41:03 +0530 Subject: [PATCH 1/4] Create MiddleOfLinkedList.java --- .../lists/MiddleOfLinkedList.java | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/main/java/com/thealgorithms/datastructures/lists/MiddleOfLinkedList.java diff --git a/src/main/java/com/thealgorithms/datastructures/lists/MiddleOfLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/MiddleOfLinkedList.java new file mode 100644 index 000000000000..c00b1127e1ca --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/lists/MiddleOfLinkedList.java @@ -0,0 +1,91 @@ +// Definition for singly-linked list node +class ListNode { + int val; // Value stored in the node + ListNode next; // Pointer to the next node + + // Constructor to initialize node with a value + ListNode(int val) { + this.val = val; + this.next = null; + } +} + +public class MiddleOfLinkedList { + + /** + * Finds the middle node of a singly linked list. + * If there are two middle nodes, returns the second one. + */ + public ListNode middleNode(ListNode head) { + // If the list is empty, just return null + if (head == null) { + return head; + } + + // Initialize two pointers: slow and fast + ListNode slow = head; // moves one step at a time + ListNode fast = head; // moves two steps at a time + + // Traverse the list + // When 'fast' reaches the end, 'slow' will be at the middle + while (fast != null && fast.next != null) { + slow = slow.next; // move slow by one node + fast = fast.next.next; // move fast by two nodes + } + + // When loop ends, slow is pointing at the middle node + return slow; + } + + /** + * Helper method to create a linked list from an array. + * Example: [1, 2, 3, 4] → 1 → 2 → 3 → 4 + */ + public static ListNode createList(int[] values) { + // If the array is empty, return null + if (values.length == 0) { + return null; + } + + // Create the head node + ListNode head = new ListNode(values[0]); + ListNode current = head; + + // Loop through the rest of the array to build the list + for (int i = 1; i < values.length; i++) { + current.next = new ListNode(values[i]); // create next node + current = current.next; // move pointer forward + } + + return head; // return the head of the linked list + } + + /** + * Helper method to print the linked list starting from any node. + * Example: prints "3 4 5" + */ + public static void printList(ListNode node) { + while (node != null) { + System.out.print(node.val + " "); // print current node value + node = node.next; // move to next node + } + System.out.println(); // print newline after list + } + + public static void main(String[] args) { + MiddleOfLinkedList sol = new MiddleOfLinkedList(); + + // Input array for the linked list + int[] values = {1, 2, 3, 4, 5}; // Odd-length list + + // Create linked list from array + ListNode head = createList(values); + + // Find middle node + ListNode middle = sol.middleNode(head); + + // Print all nodes from middle to end + System.out.print("Middle node and following nodes: "); + printList(middle); // Expected output: 3 4 5 + } +} From 226a5ccdbb93e6e66c9586874b5fbb42e0d3f6e2 Mon Sep 17 00:00:00 2001 From: AYAZ2006 Date: Sun, 5 Oct 2025 18:48:02 +0530 Subject: [PATCH 2/4] Update MiddleOfLinkedList.java --- .../lists/MiddleOfLinkedList.java | 45 +++++++------------ 1 file changed, 15 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/MiddleOfLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/MiddleOfLinkedList.java index c00b1127e1ca..585bd160305a 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/MiddleOfLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/MiddleOfLinkedList.java @@ -3,7 +3,11 @@ class ListNode { int val; // Value stored in the node ListNode next; // Pointer to the next node - // Constructor to initialize node with a value + /** + * Constructor to initialize a node with a value. + * + * @param val Value to store in the node. + */ ListNode(int val) { this.val = val; this.next = null; @@ -15,77 +19,58 @@ public class MiddleOfLinkedList { /** * Finds the middle node of a singly linked list. * If there are two middle nodes, returns the second one. + * + * @param head Head node of the linked list. + * @return Middle node of the list. */ public ListNode middleNode(ListNode head) { - // If the list is empty, just return null if (head == null) { return head; } - // Initialize two pointers: slow and fast ListNode slow = head; // moves one step at a time ListNode fast = head; // moves two steps at a time - // Traverse the list - // When 'fast' reaches the end, 'slow' will be at the middle while (fast != null && fast.next != null) { slow = slow.next; // move slow by one node fast = fast.next.next; // move fast by two nodes } - // When loop ends, slow is pointing at the middle node return slow; } /** * Helper method to create a linked list from an array. - * Example: [1, 2, 3, 4] → 1 → 2 → 3 → 4 + * + * @param values Array of integer values. + * @return Head node of the created linked list. */ public static ListNode createList(int[] values) { - // If the array is empty, return null if (values.length == 0) { return null; } - // Create the head node ListNode head = new ListNode(values[0]); ListNode current = head; - // Loop through the rest of the array to build the list for (int i = 1; i < values.length; i++) { current.next = new ListNode(values[i]); // create next node current = current.next; // move pointer forward } - return head; // return the head of the linked list + return head; } /** * Helper method to print the linked list starting from any node. - * Example: prints "3 4 5" + * + * @param node Starting node to print from. */ public static void printList(ListNode node) { while (node != null) { System.out.print(node.val + " "); // print current node value node = node.next; // move to next node } - System.out.println(); // print newline after list - } - - public static void main(String[] args) { - MiddleOfLinkedList sol = new MiddleOfLinkedList(); - - // Input array for the linked list - int[] values = {1, 2, 3, 4, 5}; // Odd-length list - - // Create linked list from array - ListNode head = createList(values); - - // Find middle node - ListNode middle = sol.middleNode(head); - - // Print all nodes from middle to end - System.out.print("Middle node and following nodes: "); - printList(middle); // Expected output: 3 4 5 + System.out.println(); } } From 5d7f67987cc893dde7dadf251a31322bafdadf86 Mon Sep 17 00:00:00 2001 From: AYAZ2006 Date: Sun, 5 Oct 2025 19:05:57 +0530 Subject: [PATCH 3/4] Update MiddleOfLinkedList.java --- .../lists/MiddleOfLinkedList.java | 50 ++++++------------- 1 file changed, 14 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/MiddleOfLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/MiddleOfLinkedList.java index 585bd160305a..63440197c21d 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/MiddleOfLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/MiddleOfLinkedList.java @@ -1,50 +1,33 @@ +package com.thealgorithms.datastructures.lists; + // Definition for singly-linked list node -class ListNode { - int val; // Value stored in the node - ListNode next; // Pointer to the next node +public class ListNode { + public int val; // Value stored in the node + public ListNode next; // Pointer to the next node - /** - * Constructor to initialize a node with a value. - * - * @param val Value to store in the node. - */ - ListNode(int val) { + public ListNode(int val) { this.val = val; this.next = null; } } public class MiddleOfLinkedList { - - /** - * Finds the middle node of a singly linked list. - * If there are two middle nodes, returns the second one. - * - * @param head Head node of the linked list. - * @return Middle node of the list. - */ public ListNode middleNode(ListNode head) { if (head == null) { return head; } - ListNode slow = head; // moves one step at a time - ListNode fast = head; // moves two steps at a time + ListNode slow = head; + ListNode fast = head; while (fast != null && fast.next != null) { - slow = slow.next; // move slow by one node - fast = fast.next.next; // move fast by two nodes + slow = slow.next; + fast = fast.next.next; } return slow; } - /** - * Helper method to create a linked list from an array. - * - * @param values Array of integer values. - * @return Head node of the created linked list. - */ public static ListNode createList(int[] values) { if (values.length == 0) { return null; @@ -54,22 +37,17 @@ public static ListNode createList(int[] values) { ListNode current = head; for (int i = 1; i < values.length; i++) { - current.next = new ListNode(values[i]); // create next node - current = current.next; // move pointer forward + current.next = new ListNode(values[i]); + current = current.next; } return head; } - /** - * Helper method to print the linked list starting from any node. - * - * @param node Starting node to print from. - */ public static void printList(ListNode node) { while (node != null) { - System.out.print(node.val + " "); // print current node value - node = node.next; // move to next node + System.out.print(node.val + " "); + node = node.next; } System.out.println(); } From 4f24c0b357a344846c26bf78f3b3d5d810c24285 Mon Sep 17 00:00:00 2001 From: AYAZ2006 Date: Sun, 5 Oct 2025 19:10:22 +0530 Subject: [PATCH 4/4] Update MiddleOfLinkedList.java --- .../lists/MiddleOfLinkedList.java | 62 ++++++++++++++----- 1 file changed, 46 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/MiddleOfLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/MiddleOfLinkedList.java index 63440197c21d..9833b46d0687 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/MiddleOfLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/MiddleOfLinkedList.java @@ -1,33 +1,58 @@ package com.thealgorithms.datastructures.lists; -// Definition for singly-linked list node -public class ListNode { - public int val; // Value stored in the node - public ListNode next; // Pointer to the next node - - public ListNode(int val) { - this.val = val; - this.next = null; +/** + * Class to find the middle node of a singly-linked list. + */ +public class MiddleOfLinkedList { + + /** + * Definition for singly-linked list node. + * Package-private class so it can be used internally. + */ + static class ListNode { + int val; // Value stored in the node + ListNode next; // Pointer to the next node + + /** + * Constructor to initialize a node with a value. + * + * @param val Value to store in the node. + */ + ListNode(int val) { + this.val = val; + this.next = null; + } } -} -public class MiddleOfLinkedList { + /** + * Finds the middle node of a singly linked list. + * If there are two middle nodes, returns the second one. + * + * @param head Head node of the linked list. + * @return Middle node of the list. + */ public ListNode middleNode(ListNode head) { if (head == null) { return head; } - ListNode slow = head; - ListNode fast = head; + ListNode slow = head; // moves one step at a time + ListNode fast = head; // moves two steps at a time while (fast != null && fast.next != null) { - slow = slow.next; - fast = fast.next.next; + slow = slow.next; // move slow by one node + fast = fast.next.next; // move fast by two nodes } return slow; } + /** + * Helper method to create a linked list from an array. + * + * @param values Array of integer values. + * @return Head node of the created linked list. + */ public static ListNode createList(int[] values) { if (values.length == 0) { return null; @@ -44,10 +69,15 @@ public static ListNode createList(int[] values) { return head; } + /** + * Helper method to print the linked list starting from any node. + * + * @param node Starting node to print from. + */ public static void printList(ListNode node) { while (node != null) { - System.out.print(node.val + " "); - node = node.next; + System.out.print(node.val + " "); // print current node value + node = node.next; // move to next node } System.out.println(); }