按照序号删除单链表中的结点
void deletenode(struct listnode* head, int n) {
int i = 1;
struct listnode* fast = head->next;
struct listnode* slow = head;
while (fast != NULL && i < n) {
slow = slow->next;
fast = fast->next;
i++;
}
if (fast == NULL) {
printf("妹找着");
return;
}
slow->next = fast->nezt;
free(fast);
}
删除单链表中的第一个key值
node* fast = head->next;
node* slow = head;
while (fast != NULL && fast->val != key) {
slow = slow->next;
fast = fast->next;
}
if (fast == NULL) {
printf("没找到值为%d的点",key);
return;
}
slow->nest = fast->next;
free(fast);
删除单链表中值为key的所有结点
node* fast = head->next;
node* slow = head;
while (fast != NULL) {
if (fast->val == key) {
slow->next = fast->next;
free(fast);
fast = slow->next;
}
else {
slow = slow->next;
fast = fast->next;
}
}
删除单链表中所有重复的节点
node* p = head->next;
node* fast,*slow;
while (p != NULL) {
slow = p;
fast = p->next;
while (fast!= NULL) {
if (p->val == fast->val) {
slow->next = fast->next;
free(fast);
fast = slow->next;
}
else {
slow = slow->next;
fast = fast->next;
}
}
p = p->next;
}