Middle node of a linked list

i am trying to return a middle node of linked-list, it works well when number of nodes are even and when number of nodes are odd, segmentation fault is all i see. initially i return slow if fast reached NULL, but it still fails for odd number of nodes.
i tried two conditions: 1. if fast reached NULL AND length of linked-list is even: return slow
2. if fast reach NULL AND length of linked-list is odd: return slow->next. i also run gdb which points the seg fault to: fast = fast->next->next; in the middle_node(node *) function. Appreciate.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>


typedef struct node
{
    // holds number of type int
    int number;
    // holds pointer to node of the same type
    struct node *next;
} node;

int count = 0;    

// insert/tuck new node into the existing linked list
node *insert(node *, int);

// delete entire linked list
bool destroy(node *);

// return size of  linked-list
int list_size(node *);

// return middle node of linked list
node *middle_node(node *);

int main(void)
{
    // declare head node
    node *list = NULL;
    
    // inserting  into linked list with a loop var 
    for (int i = 0; i < 11; i++)
    {
        list = insert(list, 1 + i);    
    }

    node *middle = NULL;
    middle = middle_node(list);
    printf("node: %p, value: %in", middle, middle->number);
    
    // destroy/free heaps
    destroy(list);
 }


// insert/tuck new node into the existing linked list
node *insert(node *head, int x)
{
    // 1 dynamically allocate space for the new node
    node *new = (node *)malloc(sizeof(node));
    // 2 check to make sure we didnt run out of memory
    if (new == NULL)
    {
        return 0; // NULL
    }
    // 3 populate and insert the node at the beginning of the linked list
    new->number = x;
    // 3B arrange/connect the pointers in the correct order
    // connect new element's next pointer to head first
    new->next = head;
    // set head to point to new element
    head = new;
    // 4 return a pointer to the new head of linked list
    return head;
}

// delete/free entire singly linked list
bool destroy(node *head)
{
    // 1 if youve reach a NULL pointer, stop
    if (head != NULL)
    {
        // 2 else: delete the rest of the list (recursively)
        destroy(head->next);
        // 3 free the current node
        free(head);
        return true;
    }
    return false;
}

// return size of linked list
int list_size(node *head)
{
    // return size of linked list
    // temp points to head of list
    node *temp = head;
    int size = 0;
    while (temp != NULL)
    {
        size += 1;
        temp = temp->next;
    }
    return size;
}

// return middle node of linked list
node *middle_node(node *head)
{
    // declare to pointers fast and slow of type node *
    if (head != NULL)
    {
        node *slow = NULL, *fast = NULL, *current = NULL;
        slow = head;
        fast = head;
        current = head;
        while (current != NULL)
        {
            count++;
            // one step at a time
            slow = slow->next;
            // two step at a time
            fast = fast->next->next;            
            
            // when fast points to last node
            if ((fast == NULL) && (list_size(head) % 2 == 1))
            {
                // return slow as the middle node 
                return slow->next;
            }
            if ((fast == NULL) && (list_size(head) % 2 == 0))
            {
                return slow;
            }

            // else update current node one step forward
            current = current->next;
        }
    }
    else
    {
        // return NULL otherwise
        return NULL;
    }
}

1

Your fast runner, fast = fast->next->next;, will try to dereference NULL in the cases the linked list contains an odd number of elements. Think about if the list contains one element only. fast->next will then be NULL and fast->next->next will then dereference NULL, which results in the program having undefined behavior.

You need to check that dereferencing fast->next returns non-NULL before going on to dereferencing fast->next->next. It could look like this:

node *middle_node(node *head) {
    node *slow = head;
    while(head && head->next) {
        slow = slow->next;
        head = head->next->next;
    }
    return slow;
}

Demo


You could make use of your list_size function to simplify this – and it goes through the list 1.5 times, just as your slow and fast runners do.

node *middle_node(node *head) {
    int size = list_size(head) / 2;
    while(size--) head = head->next;    
    return head;
}

Demo

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật