Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update solution for problem 876 #1435

Closed
wants to merge 1 commit into from
Closed
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
34 changes: 17 additions & 17 deletions leetcode/src/876.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
// Function to find the middle node of a linked list
// this contains the edge cases also
struct ListNode* middleNode(struct ListNode* head) {
// Check if the list is empty or has only one node
if (head == NULL || head->next == NULL) {
return head;
}

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

struct ListNode *middleNode(struct ListNode *head)
{
struct ListNode *fast, *slow;
fast = slow = head;
while (fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
// Use two pointers to find the middle node
while (fast != NULL && fast->next != NULL) {
fast = fast->next->next; // Move fast pointer two steps
slow = slow->next; // Move slow pointer one step
}
return slow;
}

return slow; // Slow will be at the middle node
}
Loading