• 面试经典题单链表反转


    struct node{
    	node* next;
    	T value;
    };

    方法一:常规方法

    node* reverse(node*& head)
    {
            if ( (head == Null) || (head->next == Null) ) return head;// 边界检测
            node* pNext = Null;
            node* pPrev = head;// 保存链表头节点
            node* pCur = head->next;// 获取当前节点
            while (pCur != Null)
            {
                pNext = pCur->next;// 将下一个节点保存下来
                pCur->next = pPrev;// 将当前节点的下一节点置为前节点
                pPrev = pCur;// 将当前节点保存为前一节点
                pCur = pNext;// 将当前节点置为下一节点
            }
    	head->next = NULL;
    	head = pPrev; 
    
    	return head;
    }

    方法二:递归

      node* reverse( node* pNode, node*& head)
     {
            if ( (pNode == Null) || (pNode->next == Null) ) // 递归跳出条件
            {
                head = pNode; // 将链表切断,否则会形成回环
                return pNode;
            }
    
            node* temp = reserve(pNode->next, head);// 递归
            temp->next = pNode;// 将下一节点置为当前节点,既前置节点
            return pNode;// 返回当前节点
     }
  • 相关阅读:
    sequence.c
     Link 
    转:MFC中屏蔽ESC和回车关闭对话框
    转:CWebBrowser2去除边框、滚动条、右键菜单
    VC:res协议——从模块中获取资源
    20131213
    20131212
    20131211
    20131205
    20131128
  • 原文地址:https://www.cnblogs.com/caleb/p/2036939.html
Copyright © 2020-2023  润新知