题解 | #从单向链表中删除指定值的节点#

从单向链表中删除指定值的节点

https://www.nowcoder.com/practice/f96cd47e812842269058d483a11ced4f

// HJ48-2 从单向链表中删除指定值的节点.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include<iostream>
#include<bits/stdc++.h>
#include<forward_list>

using namespace std;

int main()
{
	int head, n;
	cin >> n >> head;
	forward_list<int>list;
	list.push_front(head);
	for (int i = 1; i < n; i++)
	{
		int front, back;
		cin >> back >> front;
		auto it = find(list.begin(),list.end(), front);
		list.insert_after(it,back);
	}
	int back;
	cin >> back;
	list.remove(back);
	for (auto it = list.begin(); it != list.end(); it++)
	{
		cout << *it << " ";
	}
	return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务