Skip to content

Commit 76ee4b4

Browse files
authored
Create MiddleOfLinkedList.java
1 parent e6cb96f commit 76ee4b4

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Definition for singly-linked list node
2+
class ListNode {
3+
int val; // Value stored in the node
4+
ListNode next; // Pointer to the next node
5+
6+
// Constructor to initialize node with a value
7+
ListNode(int val) {
8+
this.val = val;
9+
this.next = null;
10+
}
11+
}
12+
13+
public class MiddleOfLinkedList {
14+
15+
/**
16+
* Finds the middle node of a singly linked list.
17+
* If there are two middle nodes, returns the second one.
18+
*/
19+
public ListNode middleNode(ListNode head) {
20+
// If the list is empty, just return null
21+
if (head == null) {
22+
return head;
23+
}
24+
25+
// Initialize two pointers: slow and fast
26+
ListNode slow = head; // moves one step at a time
27+
ListNode fast = head; // moves two steps at a time
28+
29+
// Traverse the list
30+
// When 'fast' reaches the end, 'slow' will be at the middle
31+
while (fast != null && fast.next != null) {
32+
slow = slow.next; // move slow by one node
33+
fast = fast.next.next; // move fast by two nodes
34+
}
35+
36+
// When loop ends, slow is pointing at the middle node
37+
return slow;
38+
}
39+
40+
/**
41+
* Helper method to create a linked list from an array.
42+
* Example: [1, 2, 3, 4] → 1 → 2 → 3 → 4
43+
*/
44+
public static ListNode createList(int[] values) {
45+
// If the array is empty, return null
46+
if (values.length == 0) {
47+
return null;
48+
}
49+
50+
// Create the head node
51+
ListNode head = new ListNode(values[0]);
52+
ListNode current = head;
53+
54+
// Loop through the rest of the array to build the list
55+
for (int i = 1; i < values.length; i++) {
56+
current.next = new ListNode(values[i]); // create next node
57+
current = current.next; // move pointer forward
58+
}
59+
60+
return head; // return the head of the linked list
61+
}
62+
63+
/**
64+
* Helper method to print the linked list starting from any node.
65+
* Example: prints "3 4 5"
66+
*/
67+
public static void printList(ListNode node) {
68+
while (node != null) {
69+
System.out.print(node.val + " "); // print current node value
70+
node = node.next; // move to next node
71+
}
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
90+
}
91+
}

0 commit comments

Comments
 (0)