单链表的就地反转

#include<iostream>
#include<vector>

using namespace std;

typedef struct node
{
	int element;
	struct node *next;
	struct node(int data):element(data),next(NULL){};
}Node;

Node* reverse_t(Node *head)
{
	
	Node *cur = head;
	if(cur==NULL||cur->next==NULL)
		return cur;
	Node *pNext = cur->next;

	while(pNext!=NULL)
	{
		Node *tmp = NULL;
		tmp = pNext->next;
		pNext ->next = cur;
		cur = pNext;
		pNext = tmp;
	}
	head->next=NULL;
	head = cur;
	return head;
}

int main()
{
	Node n0(0);
	Node n1(1);
	Node n2(2);
	Node n3(3);
	Node n4(4);
	n0.next = &n1;
	n1.next= &n2;
	n2.next = &n3;
	n3.next = &n4;
	Node *p = &n0;
	p = reverse_t(p);
	while(p)
	{
		cout<<p->element<<" ";
		p = p->next;
	}
}

  

原文地址:https://www.cnblogs.com/break-python/p/5391579.html