函数find_middle()实现了对给定的单链表,查找到其中间结点。如果中间结点为两个,返回前面的那个结点的地址

#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;        
}
 
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632