把單鏈表相鄰元素反轉

 

//函數功能:把鏈表相鄰元素反轉
//輸入參數: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;
    }

}
相關文章
相關標籤/搜索