从单向链表中删除指定值的节点(格式控制真坑,末尾要打印空格,样例输出有错误)
2017-06-12 18:51
323 查看
#include<iostream> using namespace std; struct ListNode { int val; ListNode* next; }; int main() { int n,head_value; while(cin>>n>>head_value) { ListNode *p,*q,*head=new ListNode; head->val=head_value; head->next=NULL; q=head; for(int i=0; i<n-1; i++) { int x,y; cin>>x>>y; p=new ListNode; p->val=x; p->next=NULL; while(q) { if(q->val==y) { p->next=q->next; q->next=p; break; } else q=q->next; } q=head; } int denum; cin>>denum; q=head; while(q) { if(q->val==denum) { if(q==head) { head=q->next; delete q; } else { p->next=q->next; delete q; } break; } else { p=q; q=q->next; } } q=head; while(q) { cout<<q->val<<' '; q=q->next; } cout<<endl; } return 0; } #include<bits/stdc++.h> using namespace std; int main() { int n; while(cin>>n) { list<int>L; list<int>::iterator it; int x,y; cin>>x; L.push_back(x); for(int i=1; i<n; i++) { cin>>x>>y; it=find(L.begin(),L.end(),y); if(it!=L.end()) { advance(it,1); L.insert(it,x); } } cin>>x; it=find(L.begin(),L.end(),x); if(it!=L.end()) L.erase(it); for(it=L.begin(); it!=L.end(); it++) { cout<<*it<<" "; } cout<<endl; } return 0; }
相关文章推荐
- 单向链表的创建、节点删除、链表输出、翻转(写法参考计蒜客数据结构课程)
- OJ-------从单向链表中删除指定节点
- 华为OJ——从单向链表中删除指定值的节点
- 24.从单向链表中删除指定值的节点
- Offer题5 链表末尾插入&删除指定值&从头到尾打印
- 华为机试:从单向链表中删除指定值的节点
- 【华为OJ】【093-从单向链表中删除指定值的节点】
- 单向链表(四) 修改指定节点的数据值、逆序输出
- 华为oj中级 从单向链表删除指定节点
- 从单向链表中删除指定值的节点
- 从单向链表中删除指定值的节点
- 从单向链表中删除指定值的节点(OJ 不用看这题)
- 华为OJ——从单向链表中删除指定值的节点
- 删除单向链表的指定节点
- 华为OJ从单向链表中删除指定值的节点
- 数据结构之链表面试题汇总(四)得到两个单链表相交的第一个交点、用O(1)的时间效率删除单向链表中的指定节点
- [编程题]从单向链表中删除指定值的节点
- 从单向链表中删除指定值的节点
- 华为OJ:从单向链表中删除指定值的节点
- 华为OJ(从单向链表中删除指定值的节点)