Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions Cpp/middle_linked_list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <iostream>
using namespace std;
class ListNode {
public:
int val;
ListNode *next;

ListNode(int x) : val(x), next(nullptr) {}
};

class LinkedList {
public:
ListNode *head;

LinkedList() : head(nullptr) {}

void append(int value) {
if (!head) {
head = new ListNode(value);
} else {
ListNode *current = head;
while (current->next) {
current = current->next;
}
current->next = new ListNode(value);
}
}

int findMiddle() {
if (!head) {
return -1; // Or any other value indicating an empty list
}

ListNode *slow = head;
ListNode *fast = head;

while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}

return slow->val;
}

// Destructor to free allocated memory
~LinkedList() {
while (head) {
ListNode *temp = head;
head = head->next;
delete temp;
}
}
};

// Function to input values from the user
void inputLinkedList(LinkedList &linkedList) {
int n, value;
cout<< "Enter the number of elements in the linked list: ";
cin>> n;

cout << "Enter the values of the linked list: ";
for (int i = 0; i < n; ++i) {
cin >> value;
linkedList.append(value);
}
}

// Example Usage:
int main() {
LinkedList linkedList;

// Input values from the user
inputLinkedList(linkedList);

int middleValue = linkedList.findMiddle();
cout << "Middle value of the linked list: " << middleValue << endl;

return 0;
}