当前位置: 首页 > news >正文

PAT 1097 Deduplication on a Linked List

个人学习记录,代码难免不尽人意
Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated absolute values of the keys. That is, for each value K, only the first node of which the value or absolute value of its key equals K will be kept. At the mean time, all the removed nodes must be kept in a separate list. For example, given L being 21→-15→-15→-7→15, you must output 21→-15→-7, and the removed list -15→15.

Input Specification:
Each input file contains one test case. For each case, the first line contains the address of the first node, and a positive N (≤10 5 ) which is the total number of nodes. The address of a node is a 5-digit nonnegative integer, and NULL is represented by −1.

Then N lines follow, each describes a node in the format:

Address Key Next
where Address is the position of the node, Key is an integer of which absolute value is no more than 10 4, and Next is the position of the next node.

Output Specification:
For each case, output the resulting linked list first, then the removed list. Each node occupies a line, and is printed in the same format as in the input.

Sample Input:
00100 5
99999 -7 87654
23854 -15 00000
87654 15 -1
00000 -15 99999
00100 21 23854
Sample Output:
00100 21 23854
23854 -15 99999
99999 -7 -1
00000 -15 87654
87654 15 -1

#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<map>
#include<set>
using namespace std;
struct node{int data;int address;int next; 
}Node[100010];int main(){int first,n,k;scanf("%d %d",&first,&n);for(int i=0;i<n;i++){int address,data,next;scanf("%d%d%d",&address,&data,&next);node a;a.address=address;a.data=data;a.next=next;Node[address]=a;}set<int> s;s.insert(abs(Node[first].data));int now=Node[first].address;int newfirst=-1;int newtemp=-1;bool flag=true;while(Node[now].next!=-1){node no=Node[now];if(s.find(abs(Node[no.next].data))==s.end()){s.insert(abs(Node[no.next].data));now=no.next;}else{Node[now].next=Node[no.next].next;if(flag){newfirst=no.next;newtemp=newfirst;flag=false;}else{Node[newtemp].next=no.next;newtemp=no.next;}}}now=Node[first].address;while(now!=-1){if(Node[now].next==-1)printf("%05d %d -1\n",Node[now].address,Node[now].data);else printf("%05d %d %05d\n",Node[now].address,Node[now].data,Node[now].next);now=Node[now].next;}if(newfirst!=-1){Node[newtemp].next=-1;int newnow=Node[newfirst].address;while(newnow!=-1){if(Node[newnow].next==-1)printf("%05d %d -1\n",Node[newnow].address,Node[newnow].data);else printf("%05d %d %05d\n",Node[newnow].address,Node[newnow].data,Node[newnow].next);newnow=Node[newnow].next;}}}

本题真的踩了不少坑,我的做法和《算法笔记》略有不同,其中要注意的地方有1.注意边界,比如当前node的next值为-1,再让node=node.next就很有可能出错,应该事先判断。一定要注意边界值。并且,我的做法还要判断是不是有要删除的节点(即原链表中是否出现了两个绝对值一样的数),如果没有的话有些操作不应该出现。

http://www.lryc.cn/news/132991.html

相关文章:

  • Flink 数据集成服务在小红书的降本增效实践
  • jellyfin使用ipv6+DDNS实现外网访问
  • Codeforces EDU 151 Div.2
  • V2board缓存投毒漏洞复现
  • 2023面试八股文 ——Java基础知识
  • 在linux系统中修改mysql数据目录
  • ORB-SLAM2学习笔记9之图像帧Frame
  • 面试热题(不同的二分搜索树)
  • MybatisPlus整合p6spy组件SQL分析
  • 项目实战 — 博客系统③ {功能实现}
  • 卷积神经网络全解:(AlexNet/VGG/ GoogLeNet/LeNet/ResNet/卷积/激活/池化/全连接)、现代卷积神经网络、经典卷积神经网络
  • WDM 模型(Windows Driver Model)简述
  • 【算法刷题之数组篇(1)】
  • 【数据挖掘】使用 Python 分析公共数据【01/10】
  • html怎么插入视频?视频如何插入页面
  • 游戏服务端性能测试
  • 【使用Zookeeper当作注册中心】自己定制负载均衡常见策略
  • 设计模式十七:迭代器模式(Iterator Pattern)
  • Python制作爱心并打包成手机端可执行文件
  • 使用docker-compose.yml快速搭建开发、部署环境(nginx、tomcat、mysql、jar包、各种程序)以及多容器通信和统一配置
  • 管理类联考——逻辑——真题篇——按知识分类——汇总篇——二、论证逻辑——支持加强——第三节——分类3——类比题干支持
  • 搜索旋转排序数组
  • Steam搬砖项目:最长久稳定的副业!
  • 最小化安装移动云大云操作系统--BCLinux-R8-U8-Server-x86_64-230802版
  • 神经网络基础-神经网络补充概念-05-导数
  • kubernetes — 安装Ingress
  • SSR使用HTTPS
  • Spring Boot中使用validator如何实现接口入参自动检验
  • thinkphp 5 实现UNION ALL 3个联表查询,并且带上搜索条件,名称,时间,手机号
  • React 之 Router - 路由详解