自定义单链表(非循环)反转的基本函数接口

题干

image

struct ListNode* ReverseList(struct ListNode* head ) {

    if (head == NULL||head->next==NULL ) {
        return head;
    } else {
        struct ListNode* Phead=head;
        struct ListNode* temp=head->next;
        Phead->next=NULL;
        Phead=temp;
        temp=temp->next;
        while (temp) {
        Phead->next=head;
        head=Phead;
        Phead=temp;
        temp=temp->next;
        }
        Phead->next=head;
        head=Phead;
    }
    return head;
}

运行结果

image

posted @ 2024-05-07 20:04  林大官人995  阅读(11)  评论(0)    收藏  举报