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