What will be the complexity of the operation to remove an element from the end of the singly linked list ? I have implemented linked-list in C. Here is the code for removing a element from the end of linked-list. Now my query is that how to calculate the complexity of this snippet. What are the factors involved. There are other operations involved too like
- Insert at begin
- Insert at middle
- Insert at end
- Delete at begin ,middle ,end
- Reverse the list
How will I be able to calculate them ?
struct node {
int val;
struct node * next;
};
typedef struct node item;
item * head = NULL;
/* DELETION AT THE END OF THE LIST */
deleteatend() {
item * temp, * curr;
if(head == NULL) {
printf("\n Linked list is Empty ");
return;
}
temp = head;
if(head->next == NULL) { /* When There is atleast 1 NODE */
head=NULL;
}
else {
while(temp->next != NULL) { /* Traversing the list upto second-last NODE */
curr=temp;
temp=temp->next;
}
curr->next =NULL; /* When TEMP points to last NODE */
}
free(temp);
}
code for Reverse the list :
/* Reverse Printing of list */
reverselist() {
item * curr, * nextnode, * prev;
curr = head;
nextnode = curr-> next; /* NEXTNODE traverses till the end of the list */
prev = NULL;
curr-> next = NULL; /* Making the first Node as Last node */
while(nextnode != NULL) {
prev = curr; /* Generally holding the last element of the reversed list */
curr = nextnode;
nextnode = curr-> next;
curr-> next = prev;
} /* End of WHILE */
head = curr;
}