已知单向链表的头结点head,写一个函数把这个链表逆序 :
struct Node
{
int data ;
Node next ;
};
typedef struct Node Node ;
答:
Node ReverseList ( Node *head )
{
if ( head == NULL || head->next == NULL )
return head;
Node *p1 = head ;
Node *p2 = p1->next ;
Node *p3 = p2->next ;
p1->next = NULL ;
while ( p3 != NULL )
{
p2->next = p1 ;
p1 = p2 ;
p2 = p3 ;
p3 = p3->next ;
}
p2->next = p1 ;
head = p2 ;
return head ;
}
网友评论