Problem
You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty.
Solution:
/*
* Complete the 'removeDuplicates' function below.
*
* The function is expected to return an INTEGER_SINGLY_LINKED_LIST.
* The function accepts INTEGER_SINGLY_LINKED_LIST llist as parameter.
*/
/*
* For your reference:
*
* SinglyLinkedListNode {
* int data;
* SinglyLinkedListNode* next;
* };
*
*/
SinglyLinkedListNode* removeDuplicates(SinglyLinkedListNode* llist) {
SinglyLinkedListNode* temp=llist;
SinglyLinkedListNode* tem=llist;
SinglyLinkedListNode* del=llist;
while (temp->next!=NULL) {
if(temp->data==temp->next->data)
{
del=temp->next;
temp->next=del->next;
delete del;
}
else {
temp=temp->next;
}
}
return tem;
}
Comments
Post a Comment