Reverse Traversing:
The following program just displays Singly Linked List in reverse using recursive function call.
void reverseTraversing(struct node *ptr){
if(ptr!=NULL){
reverseTraversing(ptr->next);
printf("%d->",ptr->data);
}
if(ptr==head){
printf("NULL");
}
}
if(ptr!=NULL){
reverseTraversing(ptr->next);
printf("%d->",ptr->data);
}
if(ptr==head){
printf("NULL");
}
}
Reverse:
The following program perform reverse of Singly Linked List.
void reverse(){
struct node *ptr1,*ptr2,*ptr3;
ptr1=head;
ptr2=ptr1->next;
ptr1->next=NULL;
while(ptr2!=NULL){
ptr3=ptr2->next;
ptr2->next=ptr1;
ptr1=ptr2;
ptr2=ptr3;
}
head=ptr1;
}
struct node *ptr1,*ptr2,*ptr3;
ptr1=head;
ptr2=ptr1->next;
ptr1->next=NULL;
while(ptr2!=NULL){
ptr3=ptr2->next;
ptr2->next=ptr1;
ptr1=ptr2;
ptr2=ptr3;
}
head=ptr1;
}
Related Programs
- Create Singly Linked List with N number of nodes and display.
- Perform insertion operation on Singly Linked List.
- Perform deletion operation on Singly Linked List.
- Reverse of Singly Linked List.
- Move last node of Singly Linked List to first.
- Swap first and last nodes in a Singly Linked List.
- Find the length of Singly Linked List.
- Find middle node of Singly Linked List