To prove safety offer- face questions 24- inverted list - list

/ * 
Title: 
	The head node is defined a function, the first input node of the linked list, the linked list output inversion inverted list. 
* / 
/ * 
Thinking: 
	Record the current node and the next pre. 
	Disconnect the current node pointer to the next, pointing pre. 
* / 
#Include <the iostream> 
#include <the cstdlib> 

the using namespace STD; 

struct ListNode { 
	int Val; 
	struct * Next ListNode; 
	ListNode (int X): 
			Val (X), Next (NULL) { 
	} 
}; 

ListNode ReverseList * ( * PHEAD ListNode) { 
    IF (PHEAD nullptr a || pHead- ==> Next == nullptr a) return PHEAD; 

    ListNode * = pre PHEAD; 
    ListNode Curr * = pHead-> Next; 
    pre-> Next = nullptr a; 
    ListNode Next * = nullptr a;  
    the while (Curr) {
        Next = curr-> Next;
        curr->next =pre;
        pre = curr;
        curr = next;
    }
    return pre;
}

int main()
{
    ListNode *node6 = new ListNode(6);
    ListNode *node5 = new ListNode(5);
    ListNode *node4 = new ListNode(4);
    ListNode *node3 = new ListNode(3);
    ListNode *node2 = new ListNode(2);
    ListNode *node1 = new ListNode(1);
    ListNode *pHead = new ListNode(0);
    pHead->next = node1;
    node1->next = node2;
    node2->next = node3;
    node3->next = node4;
    node4->next = node5;
    node5->next = node6;
    node6->next = nullptr;

    pHead = ReverseList(pHead);
    cout<<"answer"<<endl;
    while(pHead != nullptr){
        cout<<pHead->val<<endl;
        pHead = pHead->next;
    }


    cout << "Hello world!" << endl;
    return 0;
}

    

Guess you like

Origin www.cnblogs.com/buaaZhhx/p/11904751.html