#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct ListNode {
int num;
struct ListNode *next;
}Node;
Node createlist(); /裁判实现,细节不表/
Node find_middle(Node* head);
void display(Node *head);/裁判实现,细节不表/
int main()
{
Node *head,*p;
head = createlist();
p = find_middle(head);
display(p);
return 0;
}
Node* find_middle(Node* head)
{
int count = 0;
Node *p = head;
if(p == NULL)
return p;
while(p->next != NULL)
{
count++;
p = p->next;
}
p = head;
count = count/2;
while(count > 0)
{
p = p->next;
count--;
}
return p;
}
望采纳!谢谢
Node find_middle(Node **first)
{
Node *current,*p;
current = *first;
p = *first;
if(current == NULL)
{
printf("erro:null list!\n");
return FALSE;
}
else if(current->next == NULL)
return current->value;
else
current = current->next->next;
/*
**current右移的距离设为m,p右移的距离设为n
**则有:m = 2n 或 m = 2n+1.
*/
while(current)
{
p = p->next;
if(current->next != NULL)
current = current->next->next;
else
break;
}
return p->value;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!