`

链表反转

阅读更多
template<typename T>
void disorderList<T>::reverse()
{
    ListNode<T> *node = first->link;
    //这里也许用2个指针就可以,但我没想到解决办法
    ListNode<T> *preNode = NULL;
    ListNode<T> *currentNode = NULL;
    ListNode<T> *newNode = NULL;
    while(node!=NULL){
        newNode = node;
        currentNode = node;
        node = node->link;
        first->link = newNode;
        if(preNode==NULL){
            last = newNode;//尾指针
        }
        newNode->link = preNode;
        preNode = currentNode;
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics