wKiom1Zi5oKAp3j5AABKvPyuy3U294.png

wKioL1Zi5u6Chrz-AABdlJ8aA1c883.png

wKioL1Zi5u-SLUlDAABAGj7G21A589.png

wKiom1Zi5oSyS4AxAAA0y7LD6fc518.png

void reverse(struct list *ls)//链表逆置

{

    if (ls->next == NULL)

        return;//只有一个首节点,不需要逆置

 

    if (ls->next->next == NULL)

        return;//也不需要逆置

 

    struct list *last = ls->next;//逆置后ls->next就成了最后一个节点了

 

    struct list *pre = ls;//上一个节点的指针

    struct list *cur = ls->next;//当前节点的指针

    struct list *next = NULL;//下一个节点的指针

    while(cur)

    {

        next = cur->next;

        cur->next = pre;

        pre = cur;

        cur = next;

    }

 

    ls->next = pre;

    last->next = NULL;

}