编写removeduplicates()函数,以获取列表并从列表中删除所有重复节点。
列表未排序。
例如,如果链表是12->11->12->21->41->43->21,那么removeUplicates()应该将链表转换为12->11->21->41->43。
建议:请先在“实践”中解决,然后再继续解决问题。
方法1(使用两个回路)
这是使用两个循环的简单方法。外循环用于逐个选取元素,内循环将选取的元素与其余元素进行比较。
感谢Gaurav Saxena在编写此代码方面的帮助。
C++
/* Program to remove duplicates in an unsorted
linked list */
#include<bits/stdc++.h>
using namespace std;
/* A linked list node */
struct node
{
int data;
struct Node *next;
};
// Utility function to create a new Node
struct Node *newNode(int data)
{
Node *temp = new Node;
temp->data = data;
temp->next = NULL;
return temp;
}
/* Function to remove duplicates from a
unsorted linked list */
void removeDuplicates(struct Node *start)
{
struct Node *ptr1, *ptr2, *dup;
ptr1 = start;
/* Pick elements one by one */
while (ptr1 != NULL && ptr1->next != NULL)
{
ptr2 = ptr1;
/* Compare the picked element with rest
of the elements */
while (ptr2->next != NULL)
{
/* If duplicate then delete it */
if (ptr1->data == ptr2->next->data)
{
/* sequence of steps is important here */
dup = ptr2->next;
ptr2->next = ptr2->next->next;
delete(dup);
}
else /* This is tricky */
ptr2 = ptr2->next;
}
ptr1 = ptr1->next;
}
}
/* Function to print nodes in a given linked list */
void printList(struct Node *node)
{
while (node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
/* Druver program to test above function */
int main()
{
/* The constructed linked list is:
10->12->11->11->12->11->10*/
struct Node *start = newNode(10);
start->next = newNode(12);
start->next->next = newNode(11);
start->next->next->next = newNode(11);
start->next->next->next->next = newNode(12);
start->next->next->next->next->next =
newNode(11);
start->next->next->next->next->next->next =
newNode(10);
printf("Linked list before removing duplicates ");
printList(start);
removeDuplicates(start);
printf("nLinked list after removing duplicates ");
printList(start);
return 0;
}
JAVA
// Java program to remove duplicates from unsorted
// linked list
class LinkedList {
static Node head;
static class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
/* Function to remove duplicates from an
unsorted linked list */
void remove_duplicates() {
Node ptr1 = null, ptr2 = null, dup = null;
ptr1 = head;
/* Pick elements one by one */
while (ptr1 != null && != null) {
ptr2 = ptr1;
/* Compare the picked element with rest
of the elements */
while != null) {
/* If duplicate then delete it */
if == ) {
/* sequence of steps is important here */
dup = ;
= .next;
Sy();
} else /* This is tricky */ {
ptr2 = ;
}
}
ptr1 = ;
}
}
void printList(Node node) {
while (node != null) {
Sy + " ");
node = node.next;
}
}
public static void main(String args) {
LinkedList list = new LinkedList();
li = new Node(10);
li.next = new Node(12);
li.next.next = new Node(11);
li.next.next.next = new Node(11);
li.next.next.next.next = new Node(12);
li.next.next.next.next.next = new Node(11);
li.next.next.next.next.next.next = new Node(10);
Syln("Linked List before removing duplicates : n ");
li(head);
li();
Syln("");
Syln("Linked List after removing duplicates : n ");
li(head);
}
}
// This code has been contributed by Mayank Jaiswal
Output :
Linked list before removing duplicates:
10 12 11 11 12 11 10
Linked list after removing duplicates:
10 12 11
时间复杂度: O(n^2)
方法2(使用排序)
通常,Merge Sort是最适合用于有效排序链表的排序算法。
- 使用Merge Sort对元素进行排序。 我们很快就会写一篇关于排序链表的帖子。O(nLogn)
- 使用用于删除已排序的链接列表中的重复项的算法,以线性时间删除重复项。O(n)
请注意,此方法不保留元素的原始顺序。
时间复杂度:O(nLogn)
方法3(使用哈希)
我们从头到尾遍历链接列表。 对于每个新遇到的元素,我们检查它是否在哈希表中:如果是,我们将其删除; 否则我们把它放在哈希表中。
C++
/* Program to remove duplicates in an unsorted
linked list */
#include<bits/stdc++.h>
using namespace std;
/* A linked list node */
struct Node
{
int data;
struct Node *next;
};
// Utility function to create a new Node
struct Node *newNode(int data)
{
Node *temp = new Node;
temp->data = data;
temp->next = NULL;
return temp;
}
/* Function to remove duplicates from a
unsorted linked list */
void removeDuplicates(struct Node *start)
{
// Hash to store seen values
unordered_set<int> seen;
/* Pick elements one by one */
struct Node *curr = start;
struct Node *prev = NULL;
while (curr != NULL)
{
// If current value is seen before
if (curr->data) != ())
{
prev->next = curr->next;
delete (curr);
}
else
{
(curr->data);
prev = curr;
}
curr = prev->next;
}
}
/* Function to print nodes in a given linked list */
void printList(struct Node *node)
{
while (node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
/* Driver program to test above function */
int main()
{
/* The constructed linked list is:
10->12->11->11->12->11->10*/
struct Node *start = newNode(10);
start->next = newNode(12);
start->next->next = newNode(11);
start->next->next->next = newNode(11);
start->next->next->next->next = newNode(12);
start->next->next->next->next->next =
newNode(11);
start->next->next->next->next->next->next =
newNode(10);
printf("Linked list before removing duplicates : n");
printList(start);
removeDuplicates(start);
printf("nLinked list after removing duplicates : n");
printList(start);
return 0;
}
JAVA
// Java program to remove duplicates
// from unsorted linkedlist
import java.u;
public class removeDuplicates
{
static class node
{
int val;
node next;
public node(int val)
{
= val;
}
}
/* Function to remove duplicates from a
unsorted linked list */
static void removeDuplicate(node head)
{
// Hash to store seen values
HashSet<Integer> hs = new HashSet<>();
/* Pick elements one by one */
node current = head;
node prev = null;
while (current != null)
{
int curval = current.val;
// If current value is seen before
if (curval)) {
= current.next;
} else {
(curval);
prev = current;
}
current = current.next;
}
}
/* Function to print nodes in a given linked list */
static void printList(node head)
{
while (head != null)
{
Sy + " ");
head = ;
}
}
public static void main(String args)
{
/* The constructed linked list is:
10->12->11->11->12->11->10*/
node start = new node(10);
= new node(12);
.next = new node(11);
.next.next = new node(11);
.next.next.next = new node(12);
.next.next.next.next = new node(11);
.next.next.next.next.next = new node(10);
Syln("Linked list before removing duplicates :");
printList(start);
removeDuplicate(start);
Syln("nLinked list after removing duplicates :");
printList(start);
}
}
// This code is contributed by Rishabh Mahrsee
Output :
Linked list before removing duplicates:
10 12 11 11 12 11 10
Linked list after removing duplicates:
10 12 11
时间复杂度:平均为O(n)(假设哈希表访问时间平均为O(1))。
1.文章《如何 删除哈希表中的一个元素》援引自互联网,为网友投稿收集整理,仅供学习和研究使用,内容仅代表作者本人观点,与本网站无关,侵删请点击页脚联系方式。
2.文章《如何 删除哈希表中的一个元素》仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证。
相关推荐
- . 现代买票为什么带上携程保险
- . 潮阳怎么去广州南站
- . 湖南马拉河怎么样
- . 烧纸为什么到三岔路口
- . 百色为什么这么热
- . 神州租车怎么样
- . 芜湖方特哪个适合儿童
- . 护肤品保养液是什么类目
- . 早晚的护肤保养有哪些项目
- . 女孩护肤品怎么保养的最好