struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
Node* findMiddle(Node* head) {
Node* slow = head;
Node* fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
int main() {
Node* head = new Node(10);
head->next = new Node(20);
head->next->next = new Node(30);
head->next->next->next = new Node(40);
head->next->next->next->next = new Node(50);
Node* mid = findMiddle(head);
if (mid) {
std::cout << mid->data << std::endl;
}
return 0;
}The fast pointer moves twice as fast as the slow pointer. When fast reaches the end, slow is at the middle.
For 5 nodes, the middle is the 3rd node with value 30.
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
Node* findMiddle(Node* head) {
Node* slow = head;
Node* fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
int main() {
Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(4);
head->next->next->next->next = new Node(5);
head->next->next->next->next->next = new Node(6);
Node* mid = findMiddle(head);
if (mid) {
std::cout << mid->data << std::endl;
}
return 0;
}When the list has even nodes, the slow pointer ends at the second middle node.
Here, nodes are 1 to 6, so middle is node 4.
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
Node* findMiddle(Node* head) {
Node* slow = head;
Node* fast = head->next;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
int main() {
Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
Node* mid = findMiddle(head);
if (mid) {
std::cout << mid->data << std::endl;
}
return 0;
}Starting fast at head->next causes the slow pointer to stop one node earlier than usual.
For 3 nodes, the middle should be 2, which is correctly output.
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
Node* findMiddle(Node* head) {
Node* slow = head;
Node* fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
void deleteNode(Node*& head, int val) {
if (!head) return;
if (head->data == val) {
Node* temp = head;
head = head->next;
delete temp;
return;
}
Node* curr = head;
while (curr->next && curr->next->data != val) {
curr = curr->next;
}
if (curr->next) {
Node* temp = curr->next;
curr->next = curr->next->next;
delete temp;
}
}
int main() {
Node* head = new Node(5);
head->next = new Node(10);
head->next->next = new Node(15);
head->next->next->next = new Node(20);
head->next->next->next->next = new Node(25);
deleteNode(head, 15);
deleteNode(head, 5);
Node* mid = findMiddle(head);
if (mid) {
std::cout << mid->data << std::endl;
}
return 0;
}After deleting nodes with values 15 and 5, the list is 10 -> 20 -> 25.
The middle node is 20.
The fast pointer moves two steps for every one step of the slow pointer.
When fast reaches the end, slow has moved half the distance, so it is at the middle.
