把单链表相邻元素反转

//函数功能:把链表相邻元素反转
//输入参数:head:指向链表头结点

void reverse(Node* head){
    
    if (head == NULL || head->next == NULL)
        return ;

    Node *pre = head, *cur = head->next, *next = NULL;
    while (cur != NULL && cur->next != NULL) {
        next = cur->next->next;
        pre->next = cur->next;
        cur->next->next = cur;
        cur->next = next;

        pre = cur;
        cur = next;
    }

}

猜你喜欢

转载自www.cnblogs.com/fuqia/p/10262485.html