oral_quiz->#反转链表#

#include <stdio.h>
#include "Utilities/List.h"
#include <exception>

//怎么看怎么二, 还用了仨指针, 看了hht的, 感觉自己写的是shit
ListNode* MyReverseList(ListNode* pHead) {
	if(pHead == NULL) return NULL;

	//only one node
	if(pHead->m_pNext == NULL) return pHead;
	//only two node
	if(pHead->m_pNext->m_pNext == NULL) {
		ListNode* p1 = pHead->m_pNext;
		pHead->m_pNext = NULL;
		p1->m_pNext = pHead;
		return p1;
	}

	ListNode *p = pHead, *p1 = pHead->m_pNext, *p2 = p1->m_pNext;
	while(p2->m_pNext != NULL) {
		if(p == pHead) p->m_pNext = NULL;

		p1->m_pNext = p;
		p = p1; p1 = p2; p2 = p2->m_pNext;
	}

	p1->m_pNext = p;
	p2->m_pNext = p1;

	return p2;
}

//=====================MyRercursive========================
//=====================Recursive的代码还长,屁用======================
ListNode* MyReverseList_Rercursive_Help(ListNode* pPre, ListNode* pCur, ListNode** pHead);

ListNode* MyReverseList_Rercursive(ListNode* pHead) {
	if(pHead == NULL) return NULL;

	MyReverseList_Rercursive_Help(NULL, pHead, &pHead);

	return pHead;
}

ListNode* MyReverseList_Rercursive_Help(ListNode* pPre, ListNode* pCur, ListNode** pHead) {
	if(pCur->m_pNext == NULL) {
		pCur->m_pNext = pPre;
		*pHead = pCur;
		return pPre;
	}

	MyReverseList_Rercursive_Help(pCur, pCur->m_pNext, pHead)->m_pNext = pPre;
	return pPre;
}

//===============================hht's================================
ListNode* ReverseList(ListNode* pHead) {
	ListNode *pNode = pHead, *pPrev = NULL;
	while(pNode != NULL) {
		ListNode* pNext = pNode->m_pNext;

		pNode->m_pNext = pPrev;
		pPrev = pNode;
		pNode = pNext;
	}

	return pPrev;
}

//normal
void Test1() {
	printf("============Test1 starts==========\n");
	ListNode* pNode1 = CreateListNode(1);
	ListNode* pNode2 = CreateListNode(2);
	ListNode* pNode3 = CreateListNode(3);
	ListNode* pNode4 = CreateListNode(4);
	ListNode* pNode5 = CreateListNode(5);

	ConnectListNodes(pNode1, pNode2);
    ConnectListNodes(pNode2, pNode3);
    ConnectListNodes(pNode3, pNode4);
    ConnectListNodes(pNode4, pNode5);

    PrintList(pNode1);
//    ListNode* newHead = ReverseList(pNode1);
    ListNode* newHead = MyReverseList_Rercursive(pNode1);
    PrintList(newHead);

    DestroyList(newHead);
}

//two nodes
void Test2() {
	printf("============Test2 starts==========\n");
	ListNode* pNode3 = CreateListNode(3);
	ListNode* pNode4 = CreateListNode(4);

    ConnectListNodes(pNode3, pNode4);

    PrintList(pNode3);
//    ListNode* newHead = ReverseList(pNode3);
    ListNode* newHead = MyReverseList_Rercursive(pNode3);
    PrintList(newHead);

    DestroyList(newHead);}

//only one node
void Test3() {
	printf("============Test3 starts==========\n");
	ListNode* pNode5 = CreateListNode(5);

    PrintList(pNode5);
//    ListNode* newHead = ReverseList(pNode5);
    ListNode* newHead = MyReverseList_Rercursive(pNode5);
    PrintList(newHead);

    DestroyList(newHead);
}

//NULL
void Test4() {
	printf("============Test4 starts==========\n");
//    ListNode* newHead = ReverseList(NULL);
	ListNode* newHead = MyReverseList_Rercursive(NULL);
    PrintList(newHead);

    DestroyList(newHead);
}


int main(int argc, char* argv[]) {
	Test1();
	Test2();
	Test3();
	Test4();

	return 0;
}



你可能感兴趣的:(链表,反转)