6-3 链表逆置

本题要求实现一个函数,将给定单向链表逆置,即表头置为表尾,表尾置为表头。链表结点定义如下:

struct ListNode *reverse( struct ListNode *head )
{
     struct ListNode *p = head;
     struct ListNode *newhead = NULL;
    while(p)
    {
         struct ListNode * r =  (struct ListNode*)malloc(sizeof( struct ListNode));
        
        r->data = p->data ; 
        r->next = newhead;
        
        newhead  = r;
        
        p = p->next;
    }
    return newhead;
    
    

 

posted @ 2022-10-24 23:51  旺旺大菠萝  阅读(44)  评论(0编辑  收藏  举报