C语言,补全一下这个代码,其他代码不要改动

从最高点开始,向右从大到小的顺序排列,如下图
img
struct node {
int height;
struct node *next;
};

// ADD ANY FUNCTION DECLARATIONS YOU WISH TO USE HERE

struct node *mountaineer(struct node *head) {
// TODO: COMPLETE THIS FUNCTION

return head;

}
void print_and_free_list(struct node *head) {
if (head == NULL) {
printf("\n");
return;
}
printf("%d, ", head->height);
print_and_free_list(head->next);
free(head);
}

struct node *make_list(int length, char *argv[]) {
struct node *head = malloc(sizeof (struct node));
struct node *n = head;
int i = 0;
while (i < length) {
n->height = strtol(argv[i + 1], NULL, 10);
if (i < length - 1) {
// there are more nodes to make
n->next = malloc(sizeof (struct node));
n = n->next;
} else {
n->next = NULL;
}
i++;
}
return head;
}

int main(int argc, char *argv[]) {
struct node *head = make_list(argc - 1, argv);
struct node *return_path = mountaineer(head);
printf("Given stopping points: ");
print_and_free_list(head);

printf("Return stopping points: ");
print_and_free_list(return_path);
return 0;

}

十万个为什么么......
国外老师这么狠啊
先要找到最高点,然后向前遍历比他小的,再把下一个作为标杆,向前找比它小的

struct node *mountaineer(struct node *head) {
       if(head == NULL)
        return NULL;
    struct node *r = NULL;
    struct node *p = head;
    struct node *q = head->next;
    unsigned long sH = p->height;
    unsigned long mH = 0xFFFFFFFF;
    while(q != NULL)
    {
        if(q->height < sH)
        {
              p->next = q->next;
              free(q);
              q = p->next;
        }
        else if(q->height > sH && q->height < mH)
        {
              mH = q->height;
              r->next = q;
              free(p);
              p = r;
        }
        else
        {
            p->next = q->next;
        }
        r = p;
        p = q;
        q = q->next;
    }
    //
    p = head;
    q = p->next;
    p->next = NULL;
    while(q != NULL)
    {
        head = q;
        head->next = p;
        p = head;
        q = q->next;
    }
    return head;
}