编写一个函数,给定一个头指针的单链表,要求只遍历一次,将链表中的元素次序反转。

来源:互联网 发布:js return的用法 编辑:程序博客网 时间:2024/06/03 03:29
编写一个函数,给定一个头指针的单链表,要求只遍历一次,将链表中的元素次序反转。

struct node {
    int data;
    struct node* next;
};

void revertNodes(struct node* l)
{
    if (!l || !l->next = NULL) return;
    struct node* p = NULL, f = l, c = NULL;
    while (f) {
        c = f;
        f = c->next;
        c->next = p;
    }
}
0 0
原创粉丝点击