码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-08-22 19:47:58      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

  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);  
    }  
}

 

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

标签:

原文地址:http://www.cnblogs.com/xiaodi914/p/5796551.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!