Move Last Node to First

Moving last node in a Singly Linked List to first.

  • Create two node pointers: temp and lastNode
  • temp - Points to last node in actual linked list and make it as first node in resultant list.
  • lastNode - Points to last but one node in actual linked list for making as last node in resultant list.
void moveLastToFirst(){
  struct node *lastNode,*temp;
  temp=head;
  while(temp->next!=NULL){
    lastNode=temp;
    temp=temp->next;
  }
  temp->next=head;
  head=temp;
  lastNode->next=NULL;
}



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