Reverse of a Singly Linked List

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");
  }
}

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;
}



Related Programs


  1. Create Singly Linked List with N number of nodes and display.
  2. Perform insertion operation on Singly Linked List.
  3. Perform deletion operation on Singly Linked List.
  4. Reverse of Singly Linked List.
  5. Move last node of Singly Linked List to first.
  6. Swap first and last nodes in a Singly Linked List.
  7. Find the length of Singly Linked List.
  8. Find middle node of Singly Linked List

No comments:

Post a Comment

Total Pageviews