我是靠谱客的博主 笑点低缘分,这篇文章主要介绍单链表逆序算法,现在分享给大家,希望可以做个参考。

单链表逆序-----非递归

node * reverse(node *head)

{

node * cur,pnext,temp;

cur=head;

pnext=head-next;

while(cur != NULL  )

{

temp=pnext->next;             //保存pnext后面一个节点到temp

pnext->next=cur;                  //将pnext节点提到cur前面,这一步即实现了逆序

cur=pnext;                           //将cur节点指向逆序后的最前面节点

pnext=temp;                       //将pnext指向保存到temp中的节点,以便下次逆序

}

            head->next=NULL;                       ;逆序完成后完成首尾指针指向

            head=cur;

            return head;

}

 

单链表排序----递归算法

node *reverse(node * head)

{

       node *tail, newhead;

       if(head == NULL ||  head->next  == NULL)

           return head;

       tail=head->next;

      newhead=reverse(head->next);

      tail->next=head;                  //这一步实现逆序

     //逆序后设置尾指针指向

     head->next=NULL;

     return newhead;

 

}

 

最后

以上就是笑点低缘分最近收集整理的关于单链表逆序算法的全部内容,更多相关单链表逆序算法内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部