利用快慢指標快速得到連結串列中間節點

c3tc3tc3t發表於2015-05-18
//利用快慢指標快速得到連結串列中間節點
int getMiddleNodeValue(LIST * l) {
    NODE * search, * middle;
    
    search = l->head;
    middle = l->head;

    while (NULL != search->next) {
        if (NULL != search->next->next) {
            search = search->next -> next;
            middle = middle->next;
        }
        else{
            search = search->next;
        }
    }

    return middle->i;

}

 

相關文章