(程序员面试题)单链表的反转

只用一个额外的节点实现单链表的反转


testcase如下:

#include <stdio.h>
#include <stdlib.h>

typedef struct num {
    int data;
    struct num *next;
} *num_ptr;

void print_num(num_ptr head) {  
    num_ptr tmp = head;  
    while (tmp->next) {  
        printf("%d\n", tmp->next->data);  
        tmp = tmp->next;  
    }  
    printf("\n");  
}  

num_ptr reverse_link(num_ptr head) {
    num_ptr priv = head->next;
    num_ptr cur = head->next->next;
    num_ptr tmp = (num_ptr)malloc(sizeof(struct num));
    tmp->next = cur->next;
    
    priv->next = NULL;
    while (cur->next) {
        // start reverse
        cur->next = priv;

        // priv change to next
        priv = cur;

        // cur change to next, this is why need tmp node
        cur = tmp->next;

        // tmp still save cur next
        tmp->next = cur->next;
    }
    // change last node next null to priv
    cur->next = priv;
    head->next = cur;
    return head;
}

int main()
{
    num_ptr head, one, two, three, four;
    head = (num_ptr)malloc(sizeof(struct num));
    one = (num_ptr)malloc(sizeof(struct num));
    two = (num_ptr)malloc(sizeof(struct num));
    three = (num_ptr)malloc(sizeof(struct num));
    four = (num_ptr)malloc(sizeof(struct num));

    one->data = 1;
    two->data = 2;
    three->data = 3;
    four->data = 4;

    head->next = one;
    one->next = two;
    two->next = three;
    three->next = four;
    four->next = NULL;

    print_num(head);

    num_ptr new_head = reverse_link(head);
    print_num(new_head);

    return 0;
}

这个问题的关键就在于那一个额外的节点,因为当链表做完反转之后就无法指向链表的下一个节点了,这个时候用额外的节点去记住反转的节点的下一个节点,这样子就可以继续进行下一轮反转了

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值