im trying to reverse a double linked list without using any extra pointer.I only have head pointer pointing to the first node of my double linked list.
here is my function to reverse the linked list:
int reverse(){
if(head==NULL || head->next==NULL){
return 0;
}
else{
head->prev=head->next;
head->next=NULL;
head->prev->prev=head->prev->next;
head->prev->next=head;
head=head->prev;
while(head->prev==NULL){
head->prev->prev=head->prev->next;
head->prev->next=head;
head=head->prev;
}
}}
if my linked list have data 1,2,3,4,5 and after running the reverse function ,when i'm trying to display the data i'm getting output: 2,1 (instead of 5,4,3,2,1)
if(head==NULL && head->next==NULL)That will cause a NULL pointer access whenheadis a NULL. You probably want||instead of&&there. - kaylumnextandprevand when you are done, swapheadandtail? - David C. Rankinprevpointer afterwards to iterate. - David C. Rankinheadin structure the only error i have ,is in while statementwhile(head->prev==NULL)after correcting the error my program compile and works fine - taretor