Table of Contents
Example
Output
in conclusion
Home Backend Development C++ C++ program: Replace duplicate nodes in linked list with copies

C++ program: Replace duplicate nodes in linked list with copies

Aug 27, 2023 am 08:41 AM
linked list Duplicate node Replacement copy

C++ program: Replace duplicate nodes in linked list with copies

In this article, we are given a linked list containing elements from 1 to n with duplicates. Elements 1 to n will always exist with duplicates in [1..n]. We need to replace each repeated element with n 1, n 2, etc.

Let us consider an example

1→2→2→4→5→3→6→6

Next n = 42. Therefore, each duplicate is replaced by n 1, n 2, etc. The next 42 is replaced with 47, the next 46 is replaced with 48, and the first instance remains intact.

First, we need to construct a binary tree in the main method, as shown below -

Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(2);
head->next->next->next = new Node(4);
head->next->next->next->next = new Node(5);
head->next->next->next->next->next = new Node(3);
head->next->next->next->next->next->next = new Node(6);
head->next->next->next->next->next->next->next = new Node(6);
solve(head);
Copy after login

Now the nodes are as follows;

1→2→7→4→5→3→6→8

As we have observed, we need to keep track of the elements we see to find the starting value of n. We also need a mechanism to determine which elements are repeated to replace their values.

The proposed data structure that immediately comes to mind has been identified. We can iterate over the linked list and push elements into the collection. After pushing the elements from the set, we can find the last element, which is the largest element available in the linked list, since sets are sorted data structures.

In the second traversal of the linked list, we can use the set as a hash. We will search for every element in the collection, two situations may occur.

  • We find the element in the collection, then remove it from the collection and mark it.

  • We did not find the element in the set, which means we have seen it from option one and we will replace it with the next n.

Example

To replace nodes with duplicate values ​​in a linked list, please follow the C program below. The C implementation utilizes a collection data structure to traverse the linked list, thus facilitating the search for duplicate elements.

#include <iostream>
#include <set>
using namespace std;
class Node {
   public:
   int value;
   Node* next;
   Node(int value) {
      this->value = value;
      next = NULL;
   }
};
void solve(Node* head) {
   set<int> hash;
   Node* copy = head;
   while(copy) {
      hash.insert(copy->value);
      copy = copy->next;
   }
   auto it = hash.end();
   it--;
   int startingN = *it +1;
   while(head) {
      if(hash.find(head->value) != hash.end()) {
         hash.erase(head->value);
      } else {
         head->value = startingN++;
      }
      head = head->next;
   }
}
void printList(Node* head) {
   while(head) {
      cout << head->value << " ";
      head = head->next;
   }
}
int main() {
   Node* head = new Node(41);
   head->next = new Node(42);
   head->next->next = new Node(42);
   head->next->next->next = new Node(44);
   head->next->next->next->next = new Node(45);
   head->next->next->next->next->next = new Node(43);
   head->next->next->next->next->next->next = new Node(46);
   head->next->next->next->next->next->next->next = new Node(46);
   cout << "Before: ";
   printList(head);
   cout << "\n";
   solve(head);
   cout << "After: ";
   printList(head);
   return 0;
}
Copy after login

Output

Before: 41 42 42 44 45 43 46 46
After: 41 42 47 44 45 43 46 48  
Copy after login

in conclusion

We used the concept of hashing and found the largest element in the linked list with the help of a set of data structures. We can also use map or unordered_map as hash.

The above is the detailed content of C++ program: Replace duplicate nodes in linked list with copies. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Find the nth node from the last linked list in C++ using recursive method Find the nth node from the last linked list in C++ using recursive method Sep 15, 2023 pm 05:53 PM

Find the nth node from the last linked list in C++ using recursive method

Comparison of algorithm time complexity of PHP arrays and linked lists Comparison of algorithm time complexity of PHP arrays and linked lists May 07, 2024 pm 01:54 PM

Comparison of algorithm time complexity of PHP arrays and linked lists

Add 1 to a number represented by a linked list Add 1 to a number represented by a linked list Aug 29, 2023 pm 09:17 PM

Add 1 to a number represented by a linked list

PHP SPL data structures: Inject speed and flexibility into your projects PHP SPL data structures: Inject speed and flexibility into your projects Feb 19, 2024 pm 11:00 PM

PHP SPL data structures: Inject speed and flexibility into your projects

PHP data structure: the charm of linked lists, exploring dynamic data organization PHP data structure: the charm of linked lists, exploring dynamic data organization Jun 04, 2024 pm 12:53 PM

PHP data structure: the charm of linked lists, exploring dynamic data organization

How to implement linked list operations in Go language? How to implement linked list operations in Go language? Jun 10, 2023 pm 10:55 PM

How to implement linked list operations in Go language?

Python program: add elements to first and last position of linked list Python program: add elements to first and last position of linked list Aug 23, 2023 pm 11:17 PM

Python program: add elements to first and last position of linked list

How to implement linked list in golang How to implement linked list in golang Dec 14, 2023 pm 04:37 PM

How to implement linked list in golang

See all articles