按照序号删除单链表中的结点

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;

}

技术
下载桌面版
GitHub
Gitee
SourceForge
百度网盘(提取码:draw)
云服务器优惠
华为云优惠券
腾讯云优惠券
阿里云优惠券
Vultr优惠券
站点信息
问题反馈
邮箱:[email protected]
吐槽一下
QQ群:766591547
关注微信