zoukankan      html  css  js  c++  java
  • 单链表的就地反转

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

      

  • 相关阅读:
    MongoDB小结25
    MongoDB小结24
    MongoDB小结23
    MongoDB小结22
    MongoDB小结21
    MongoDB小结20
    MongoDB小结19
    MongoDB小结18
    hdu 4606 Occupy Cities
    hdu 4610 Cards
  • 原文地址:https://www.cnblogs.com/break-python/p/5391579.html
Copyright © 2011-2022 走看看