头插法翻转链表

头插法翻转链表

#include <stdio.h>
#include <stdlib.h>

struct Node;
typedef struct Node *PtrNode;
struct Node
{
    int value;
    PtrNode Next;
};

// 头插法翻转单向链表,把每一个节点插在头结点之后
PtrNode InvertList( PtrNode Head )
{
    
    if( Head->Next == NULL )
        return Head;
    
    PtrNode Cur,Tmp;
    Cur = Head->Next;
    Head->Next = NULL; //保证第一个节点不会自己指向自己
    
    while( Cur!= NULL )
    {
        Tmp = Cur->Next;
        Cur->Next = Head->Next;
        Head->Next = Cur;
        Cur = Tmp;
    }
    
    
    return Head;
}

原文地址:https://www.cnblogs.com/evansyang/p/5919375.html