单向链表的逆转

// 单向链表的逆转
#define elem_type int
typedef struct _node {
    elem_type data;
    struct _node *next;
} Node;

Node* reverse(Node* L)
{
    Node *p, *q, *t;
    p = L;
    q = NULL;
    
    // 将 p 的每个节点插到 q 的头
    while (p! = NULL){
        t = p->next;    // 记下 p 的下一个节点
        p->next = q;    // 将 P 节点插入到 q 的头
        q = p;  // 让 q 重新指向头节点
        p = t;  // 让 p 指向他的下一个节点
    }
    
    return q;
}

你可能感兴趣的:(单向链表的逆转)