最后两个测试数据TLE,求调
  • 板块B3631 单向链表
  • 楼主mogeng
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/2/7 17:28
  • 上次更新2025/2/7 20:15:38
查看原帖
最后两个测试数据TLE,求调
1519244
mogeng楼主2025/2/7 17:28
#include <iostream>
using namespace std;
struct listnode{
    int val;
    listnode *next;
    listnode(int x):val(x),next(NULL) {}
};
listnode *find(listnode *head,int target){
	while(head!=NULL){
		if(head->val==target)
			return head;
		head=head->next;
	}
	return NULL;
}
void insert(listnode *n0,listnode *p){
	listnode *n1=n0->next;
	p->next=n1;
	n0->next=p;
}
void remove(listnode *n0){
	if(n0->next==NULL)
		return;
	listnode *p=n0->next;
	listnode *n1=p->next;
	n0->next=n1;
	delete p;
}
int main(void){
    int q;
    cin>>q;
    listnode *head=new listnode(1);
    for (int i=0;i<q;i++){
        int op,x,y;
        cin >> op;
        if(op==1){
        	cin>>x>>y;
        	listnode *p=new listnode(y);
        	listnode *n0=find(head,x);
        	insert(n0,p);
		}
		else if(op==2){
			cin>>x;
			listnode *target=find(head,x);
			if(target->next!=NULL)
				cout<<target->next->val<<endl;
			else
				cout<<"0"<<endl;
		}
		else if(op==3){
			cin>>x;
			listnode *n0=find(head,x);
			remove(n0);
		}
	}
    return 0;
}

最后两个测试数据TLE

2025/2/7 17:28
加载中...