我正在尝试从链接列表中删除重复项。用户插入一个值,然后程序将检查用户的输入和链表中的值是否相同。如果相似,它将删除并在链表中只留下一个。例如链接列表=10 100。用户=10。结果=10 100 而不是 10 10 100。
int insertSorted(LinkedList *ll, int item)
{
ListNode *cur = ll->head;
int size = ll->size;
int i;
for (i = 0; i <= size; i++)
{
if ((size - i) == 0 || item < cur->item)
{
insertNode(ll, i, item); // function to insert the value into Linkedlist
return i;
}
cur = cur->next;
}
ListNode *current = ll->head;
while (current->next != NULL)
{
if (current->item == current->next->item)
{
ListNode *nextNext = current->next->next;
free(current->next);
current->next = nextNext;
}
else
{
current = current->next; // only advance if no deletion
}
}
return -1;
}
最佳答案
当您插入新节点(示例中的值 10)时,您将 return i;
,制作其余的代码,我认为它会检查重复项而不执行。
关于C 程序 - 从 LinkedList 中删除重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46865787/