每日一道面试题-01

题目:如何实现一个高效的单向链表逆序输出

思路:对链表的逆序输出,需要3个指针分别指向pre cur 和cur的next,对第一次遍历,把当前节点的next指向上一个节点,而需要用指针保存下一个节点(默认head的data无数据)

代码

typedef struct node
{
int data; struct node *next; }LinkNode; void reverse(LinkNode *head) { if (head == null || head -> next == null)
    {
   return; }
LinkNode * pre,*cur,*next;
    pre = null;
cur = head->next;
while(cur != null)
{
if (cur ->next == null)
{
        cur -> next = pre;
break;
      }
next = cur -> next;
cur -> next = pre;
pre = cur;
cur = next;
}
head ->next = cur;
LinkNode *temp = head->next;
while (temp != null)
{
printf("%d ",temp->data);
temp = temp ->next;
}
}

 

转载于:https://www.cnblogs.com/heyjia/p/11243087.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值