Assuming that the linked list is already in sorted order, what is below function doing?
void check(struct node* head)
struct node* current = head;
if (current == NULL) return; // do nothing if the list is empty
// Compare current node with next node
if (current->data == current->next->data)
struct node* nextNext = current->next->next;
current->next = nextNext;
current = current->next; // advance
It is trying to delete the duplicate nodes.
it is trying to delete the nodes with same address.
it is trying to find the middle node of this singly linked list.
it is trying to delete every alternative node of this singly linked list.
This is the logic to delete duplicate nodes from the linked list.In the while loop we are checking if next node is there or not. If next node exists, we are comparing current node data and next node data. if they are equal we are freeing next node (i.e deleting next node) and adjusting the linked list links accordingly. If data is not equal then we are proceeding further.
Back To Top