1、链表定义
typedef struct ListElement_t_ { void *data; struct ListElement_t_ *next; } ListElement_t; typedef struct List_t_{ int size; int capacity; ListElement_t *head; ListElement_t *tail; } List_t;
2、查找链表中间节点,设置快慢指针,快指针一次走两步,慢指针一次走一步
void *SearchMidNode( List_t *list){ if ( list == NULL || list->head == NULL ) return NULL; ListElement_t *pFast = list->head; ListElement_t *pSlow = list->head; //若节点为N个, N为奇数,则返回第N/2 +1个节点, N为偶数,则返回第N/2个节点 while( pFast->next != NULL && pFast->next->next != NULL){ pFast = pFast->next->next; pSlow = pSlow->next; } return pSlow->data; }
时间: 2024-10-18 15:29:03