@@ -3,7 +3,11 @@ class ListNode {
33 int val ; // Value stored in the node
44 ListNode next ; // Pointer to the next node
55
6- // Constructor to initialize node with a value
6+ /**
7+ * Constructor to initialize a node with a value.
8+ *
9+ * @param val Value to store in the node.
10+ */
711 ListNode (int val ) {
812 this .val = val ;
913 this .next = null ;
@@ -15,77 +19,58 @@ public class MiddleOfLinkedList {
1519 /**
1620 * Finds the middle node of a singly linked list.
1721 * If there are two middle nodes, returns the second one.
22+ *
23+ * @param head Head node of the linked list.
24+ * @return Middle node of the list.
1825 */
1926 public ListNode middleNode (ListNode head ) {
20- // If the list is empty, just return null
2127 if (head == null ) {
2228 return head ;
2329 }
2430
25- // Initialize two pointers: slow and fast
2631 ListNode slow = head ; // moves one step at a time
2732 ListNode fast = head ; // moves two steps at a time
2833
29- // Traverse the list
30- // When 'fast' reaches the end, 'slow' will be at the middle
3134 while (fast != null && fast .next != null ) {
3235 slow = slow .next ; // move slow by one node
3336 fast = fast .next .next ; // move fast by two nodes
3437 }
3538
36- // When loop ends, slow is pointing at the middle node
3739 return slow ;
3840 }
3941
4042 /**
4143 * Helper method to create a linked list from an array.
42- * Example: [1, 2, 3, 4] → 1 → 2 → 3 → 4
44+ *
45+ * @param values Array of integer values.
46+ * @return Head node of the created linked list.
4347 */
4448 public static ListNode createList (int [] values ) {
45- // If the array is empty, return null
4649 if (values .length == 0 ) {
4750 return null ;
4851 }
4952
50- // Create the head node
5153 ListNode head = new ListNode (values [0 ]);
5254 ListNode current = head ;
5355
54- // Loop through the rest of the array to build the list
5556 for (int i = 1 ; i < values .length ; i ++) {
5657 current .next = new ListNode (values [i ]); // create next node
5758 current = current .next ; // move pointer forward
5859 }
5960
60- return head ; // return the head of the linked list
61+ return head ;
6162 }
6263
6364 /**
6465 * Helper method to print the linked list starting from any node.
65- * Example: prints "3 4 5"
66+ *
67+ * @param node Starting node to print from.
6668 */
6769 public static void printList (ListNode node ) {
6870 while (node != null ) {
6971 System .out .print (node .val + " " ); // print current node value
7072 node = node .next ; // move to next node
7173 }
72- System .out .println (); // print newline after list
73- }
74-
75- public static void main (String [] args ) {
76- MiddleOfLinkedList sol = new MiddleOfLinkedList ();
77-
78- // Input array for the linked list
79- int [] values = {1 , 2 , 3 , 4 , 5 }; // Odd-length list
80-
81- // Create linked list from array
82- ListNode head = createList (values );
83-
84- // Find middle node
85- ListNode middle = sol .middleNode (head );
86-
87- // Print all nodes from middle to end
88- System .out .print ("Middle node and following nodes: " );
89- printList (middle ); // Expected output: 3 4 5
74+ System .out .println ();
9075 }
9176}
0 commit comments