pta:查找链表中的最大值

pta:查找链表中的最大值
我是一名大一新生,链表还懵懵懂懂,请各位指点迷津
已知head指向一个带头结点的单向链表,链表每个结点包含整型数据域data(注:data>0)和指针域next。请编写函数int max(struct node *head),在链表中查找数据域值最大的结点,由函数值返回找到的最大值。
在main( )函数中构建该链表,并调用max()查找最大值并输出.
输入样例:
1 2 3 4 5
输出样例:
Max=5

img

随便写的 没测试 不知道对不

int max(struct node *head)
{    

    struct node * p=head->next;
    int max=p->data;
    while(p->next!=NUll)
    {
        if(max<p->data)
            max=p->data;
        p=p->next;
    } 
    return max;
} 

供参考:

int max(struct node *head)
{
    struct node *p = head->next;
    int max = p->data;
    while(p){
        if (max < p->data)
            max = p->data;
        p = p->next;
    }
    return max;
}