题解:我们可以设立两个指针p和q,初始都指向头指针head,p每次移动两个位置,q每次移动一个位置,当p->next=NULL的时候,q指向的位置就是中间位置元素。
代码:
typedef struct node
{
char data;
struct node *next;
}*LinkList;
LinkList search(LinkList head)
{
LinkList temp, mid;
temp = head;
if(head==NULL)
return NULL;
while(NULL !=head->next && NULL != head->next->next)
{
head = head->next->next;
temp = temp->next;
}
mid = temp;
return mid;
}
亲测有效哦
在一次遍历中,怎么找到单链表的中间元素?














网友评论