L2-2. 链表去重

时间限制
300 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越
给定一个带整数键值的单链表L,本题要求你编写程序,删除那些键值的绝对值有重复的结点。即对任意键值K,只有键值或其绝对值等于K的第一个结点可以被保留。同时,所有被删除的结点必须被保存在另外一个链表中。例如:另L为21→-15→-15→-7→15,则你必须输出去重后的链表21→-15→-7、以及被删除的链表-15→15。

输入格式:

输入第一行包含链表第一个结点的地址、以及结点个数N(<= 105 的正整数)。结点地址是一个非负的5位整数,NULL指针用-1表示。

随后N行,每行按下列格式给出一个结点的信息:

Address Key Next

其中Address是结点的地址,Key是绝对值不超过104的整数,Next是下一个结点的地址。

输出格式:

首先输出去重后的链表,然后输出被删除结点组成的链表。每个结点占一行,按输入的格式输出。

输入样例:
00100 5
99999 -7 87654
23854 -15 00000
87654 15 -1
00000 -15 99999
00100 21 23854
输出样例:
00100 21 23854
23854 -15 99999
99999 -7 -1
00000 -15 87654
87654 15 -1

做过的一道练习题,做着做着感觉这种去重的思路可以借鉴到别的去重上面去,就决定记录一下

 1 #include<iostream>
 2 #include<math.h>
 3 #include<iomanip>
 4 using namespace std;
 5 struct Node{
 6     int value;
 7     int next;
 8 };
 9 Node m[100001];            //这四个全部放在main函数里会栈溢出,索性全部设为全局变量
10 int x[100001];                 //遍历到值的绝对值没重复的,把地址号放到x数组里,重复了的放到y数组里,这样只需要遍历一次即可
11 int y[100001];
12 bool isVisited[100001] = { 0 };      //判断值的绝对值有没有重复
13 int main(){
14     int N;
15     int start;
16     cin >> start >> N;
17     for (int i = 0; i < N; i++){
18         int adress, value, next;
19         cin >> adress >> value >> next;
20         m[adress].value = value;
21         m[adress].next = next;
22     }
23
24     int indexX = 0;
25     int indexY = 0;
26     for (int j = start; j != -1; j = m[j].next){
27         if (!isVisited[abs(m[j].value)]){
28             x[indexX++] = j;
29             isVisited[abs(m[j].value)] = true;
30         }
31         else{
32             y[indexY++] = j;
33         }
34     }
35     for (int i = 0; i < indexX; i++){
36         if (i<indexX-1)
37             cout << setw(5) << setfill(‘0‘) << x[i] << " " << m[x[i]].value << " " << x[i + 1] << endl;
38         else
39             cout << setw(5) << setfill(‘0‘) << x[i] << " " << m[x[i]].value << " " << -1 << endl;
40     }
41     for (int j = 0; j < indexY; j++){
42         if (j<indexY - 1)
43             cout << setw(5) << setfill(‘0‘) << y[j] << " " << m[y[j]].value << " " << y[j + 1] << endl;
44         else
45             cout << setw(5) << setfill(‘0‘) << y[j] << " " << m[y[j]].value << " " << -1 << endl;
46     }
47     return 0;
48 }
时间: 2024-08-26 20:25:42

L2-2. 链表去重的相关文章

天梯 L2 链表去重

L2-002 链表去重 (25 分) 给定一个带整数键值的链表 L,你需要把其中绝对值重复的键值结点删掉.即对每个键值 K,只有第一个绝对值等于 K 的结点被保留.同时,所有被删除的结点须被保存在另一个链表上.例如给定 L 为 21→-15→-15→-7→15,你需要输出去重后的链表 21→-15→-7,还有被删除的链表 -15→15. 输入格式: 输入在第一行给出 L 的第一个结点的地址和一个正整数 N(≤10?5??,为结点总数).一个结点的地址是非负的 5 位整数,空地址 NULL 用 −

LeetCode OJ:Remove Duplicates from Sorted List II(链表去重II)

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2->3.

LeetCode OJ:Remove Duplicates from Sorted List (排好序的链表去重)

Given a sorted linked list, delete all duplicates such that each element appear only once. For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3. 简单的链表去重而已啊,遍历一边就实现了: 1 class Solution { 2 public: 3 ListNode*

团体程序设计天梯赛-练习集 L2-2. 链表去重 数组模拟链表

L2-2. 链表去重 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个带整数键值的单链表L,本题要求你编写程序,删除那些键值的绝对值有重复的结点.即对任意键值K,只有键值或其绝对值等于K的第一个结点可以被保留.同时,所有被删除的结点必须被保存在另外一个链表中.例如:另L为21→-15→-15→-7→15,则你必须输出去重后的链表21→-15→-7.以及被删除的链表-15→15. 输入格式: 输入第一行包含链表第一个结

C# 链表去重 List 一维 二维 分别使用 Distinct() GroupBy() 方法

分别使用过List中Distinct(),GroupBy()实现链表的去重. 1.先上效果: 一维链表中分别有元素"aa","bb",'aa','aa',"cc",使用Distinct()方法后输出 aa,bb,cc 二维链表中类型为ClassA类型,其中对象的属性A分别为1,1,2,3,1,使用GroupBy()方法实则是分类,输出Key值分别为1,2,3. 2.上代码,类ClassA 1 class ClassA 2 { 3 private

[leetcode]83. Remove Duplicates from Sorted List有序链表去重

Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1: Input: 1->1->2 Output: 1->2 Example 2: Input: 1->1->2->3->3 Output: 1->2->3 题意: 有序链表去重 思路: 代码: 1 class Solution { 2 public Lis

谷歌笔试题 --- 环状链表去重

编码实现环状单向链表(尾指针直接指向头指针,中间没有空节点),去除连续的重复元素的操作. 比如:1(头)->2->2->3->3->1->1(头) 去除以后的结果是1->2->3,注意头尾的1也要去掉一个. //时间复杂度为O(N) //空间复杂度为O(1) //代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <cstdl

链表去重

Write code to remove duplicates from an unsorted linked list FOLLOW UPHow would you solve this problem if a temporary buffer is not allowed? 题解如下: 解决此题关键是了解java reference及链表基本知识 链表类: public class LinkedListNode { LinkedListNode next=null; int data; p

链表去重处理(去除链表中重复出现的节点)

#include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct stu{ int m; struct stu *l; }st; int main() { int m; st *h; while(scanf("%d",&m)!=EOF) { st *l; h=new st; l=new st; l=h; while(m--) { st *s; s=new st; sc