单链表反转--递归非递归

  ListNode* ReverseList(ListNode* pHead) {
       

        if(pHead==NULL)
            return NULL;
        if(pHead->next==NULL)
            return pHead;
       
        ListNode* preNode=pHead;
       //ListNode* ReseNode=NULL;
        ListNode* pNode=pHead->next;
        ListNode* NextNode=NULL;
        
        while(pNode->next!=NULL)
            {
            //ListNode* NextNode=pNode->next;
            //if(NextNode==NULL)
               // ReseNode=pNode;
            NextNode=pNode->next;
            pNode->next=preNode;
            preNode=pNode;
            pNode=NextNode;
        }
        //NextNode->next=pNode;
        pHead->next = NULL;
        pNode->next=preNode;
        return pNode;
        
    }
void ReverseList(List* list)  
{  
    Node* p_pre = NULL;  
    Node* p_cur = NULL;  
    Node* p_nxt = NULL;  
    if(NULL == list)  
        return;  
    p_cur = (*list)->next;  
    p_pre = *list;  
    p_pre->next = NULL;  
    while(p_cur) {  
        p_nxt = p_cur->next;  
        p_cur->next = p_pre;  
        p_pre = p_cur;  
        p_cur = p_nxt;  
    }  
    *list = p_pre;  
}  
Node* Reverse2Point(List* head)   
{  
    Node* p_cur = NULL;  
    Node* p_nxt = NULL;  
    if(NULL == *head)  
        return;  
    p_cur = (*head)->next;  
    (*head)->next = NULL;  
    while(p_cur) {  
        p_nxt = p_cur->next;  
        p_cur->next = *head;  
        *head = p_cur;  
        p_cur = p_nxt;  
    }  
} 
Node* Reverse(Node* p_cur, Node* p_pre)  
{  
    if(NULL == p_cur->next) {  
        p_cur->next = p_pre;  
        return p_cur;  
    }  
    else {  
        Node *p_nxt = p_cur->next;  
        p_cur->next = p_pre;  
        Reverse(p_nxt, p_cur);  
    }  
}

 

posted @ 2016-08-22 18:07  随风9  阅读(291)  评论(0编辑  收藏  举报