链表节点删除,只有标记待删除节点的前驱节点即可; [注]:如果不是带有节点设置一个虚拟节点即可,返回时返回dummy->next。
代码语言:javascript复制struct node {
int val;
node *next;
};
void delInterval(node *head, int minx, int maxx) {
if (!head) return head;
node *pre = head;
node *p = pre->next; //工作指针
while (p) {
if (minx < p->val && p->val < maxx) {
//满足条件,p为待删除节点
node *del = p;
pre->next = p->next;
p = p->next;
delete del;
} else {
pre = pre->next;
p = p->next;
}
}
return head;
}