From ea041c3583a7009552906a26f710adb2b38552fc Mon Sep 17 00:00:00 2001 From: AYAZ2006 Date: Sun, 5 Oct 2025 18:27:48 +0530 Subject: [PATCH 1/3] Create MiddleOfLinkedList.java --- .../lists/MiddleOfLinkedList.java | 87 +++++++++++++++++++ 1 file changed, 87 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..60d177839f9b --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/lists/MiddleOfLinkedList.java @@ -0,0 +1,87 @@ +// 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 Solution { + + /** + * 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) { + Solution sol = new Solution(); + + // 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 dad528b56bc9cd578c82cc9c935a8b9c868e45fe Mon Sep 17 00:00:00 2001 From: AYAZ2006 Date: Sun, 5 Oct 2025 18:31:39 +0530 Subject: [PATCH 2/3] Update MiddleOfLinkedList.java --- .../thealgorithms/datastructures/lists/MiddleOfLinkedList.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/MiddleOfLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/MiddleOfLinkedList.java index 60d177839f9b..be0c2ceeb0d5 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/MiddleOfLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/MiddleOfLinkedList.java @@ -10,7 +10,7 @@ class ListNode { } } -public class Solution { +public class MiddleOfLinkedList { /** * Finds the middle node of a singly linked list. From 0813ba4720fa41557c4d8a8023b7f61d8caf97a3 Mon Sep 17 00:00:00 2001 From: AYAZ2006 Date: Sun, 5 Oct 2025 18:34:12 +0530 Subject: [PATCH 3/3] Update MiddleOfLinkedList.java --- .../thealgorithms/datastructures/lists/MiddleOfLinkedList.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/MiddleOfLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/MiddleOfLinkedList.java index be0c2ceeb0d5..6c1641816d7b 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/MiddleOfLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/MiddleOfLinkedList.java @@ -69,7 +69,8 @@ public static void printList(ListNode node) { } public static void main(String[] args) { - Solution sol = new Solution(); + // Create an instance of this class to access non-static methods + MiddleOfLinkedList sol = new MiddleOfLinkedList(); // Input array for the linked list int[] values = {1, 2, 3, 4, 5}; // Odd-length list