(C语言)链表的反转(一次遍历)

/**

*Node为节点类型,root保存的是第一个节点的地址

*/


Node * reverse_linklist(Node *root)       //传入根节点

{

          Node *current;

          Node *pnext;

/*遍历开始current记录当前位置,pnext记录下一位置*/

          current = root;

          pnext = root->next;

/*********判断链表是否为空*********/

if(root == NULL)


{


            fprintf(stderr,"the link is empty!\n");

            return NULL;

}

current->next = NULL;             /*将第一个节点指针域置为空*/

/***************实现反转*********************/

while(pnext != NULL)

{

       root = pnext->next;//跟新root为pnext的下一节点

      pnext->next  = current;//跟新pnext->为它的上一节点即current

       current = pnext;          

       pnext = root;

}

root = current;


return root;

}

发布了4 篇原创文章 · 获赞 6 · 访问量 1万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章