Remove duplicates from sorted linked list using recursion
A linked list is a sequence of elements connected together. Each list has a header and a sequence of nodes, each of which holds data for the current node and links to the next node. The basic operations of linked lists are insertion, deletion, search and deletion.
Remove duplicates from sorted linked list
One way to delete nodes is to use recursion. The idea is to compare each node with its neighboring nodes and remove duplicate nodes where they are equal.
Our recursive call will return to the next node. So for the next element we will call the recursive function like current_node->next = our_function(node->next).
We trust our recursion and current_node->next now contains the linked list without any duplicate elements.
In the main method we bid on the list from scratch -
Node* head = new Node(5); head->next = new Node(5); head->next->next = new Node(6); head->next->next->next = new Node(7); head->next->next->next->next = new Node(7); head->next->next->next->next->next = new Node(7);
algorithm
The process of using recursion to remove duplicates from a sorted linked list is as follows.
Step 1 - Create a linked list with all values sorted in order
Step 2 - If the linked list does not exist, the program terminates.
Step 3 - If the linked list does exist, compare the next value of the head node with the value in the head node. If both values are the same, remove the header.
Step 4 - Step 3 is performed recursively, treating each node as a head until the list removes all duplicate values from itself .
Step 5 - The output obtained is a sorted linked list with different values
Example
For example, we have a sorted linked list with the following values -
1->1->1->2->3->3->4
Let's look at a C program that will remove duplicates from the above sorted linked list using recursion -
#include <iostream> using namespace std; class Node { public: int data; Node* next; Node(int data) { this->data = data; next = NULL; } }; Node* solve(Node* head) { if (head == NULL) return NULL; head->next = solve(head->next); if (head->next != NULL && head->next->data == head->data) { Node* temp = head->next; delete head; return temp; } return head; } void printList(Node* node) { while (node != NULL) { cout << node->data << (node->next == NULL ? "" : "->"); node = node->next; } } int main() { Node* head = new Node(1); head->next = new Node(1); head->next->next = new Node(1); head->next->next->next = new Node(2); head->next->next->next->next = new Node(3); head->next->next->next->next->next = new Node(3); head->next->next->next->next->next->next = new Node(4); cout << "Linked list before: "; printList(head); head = solve(head); cout << "\nLinked list after: "; printList(head); return 0; }
After that, we check whether the current node is included in the linked list. If the satisfying linked list we get from current node -> next node has the same value as that node, we don't include that node; otherwise, we include it.
Note - When the current node is NULL, we return the base condition of the recursion.
Output
Linked list before: 1->1->1->2->3->3->4 Linked list after: 1->2->3->4
in conclusion
As we saw with the recursive call, we trust that the next call will achieve the expected result of the rest of the problem. We just solved the current subproblem. With this in mind, we check if the current element can be contained and hand the rest of the linked list over to our recursive call, trusting it to give us a valid linked list from that point on. When we traverse the entire linked list, the time complexity of our method is O(n).
The above is the detailed content of Remove duplicates from sorted linked list using recursion. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The recursion depth of C++ functions is limited, and exceeding this limit will result in a stack overflow error. The limit value varies between systems and compilers, but is usually between 1,000 and 10,000. Solutions include: 1. Tail recursion optimization; 2. Tail call; 3. Iterative implementation.

1. First of all, it is false to block and delete someone permanently and not add them permanently. If you want to add the other party after you have blocked them and deleted them, you only need the other party's consent. 2. If a user blocks someone, the other party will not be able to send messages to the user, view the user's circle of friends, or make calls with the user. 3. Blocking does not mean deleting the other party from the user's WeChat contact list. 4. If the user deletes the other party from the user's WeChat contact list after blocking them, there is no way to recover after deletion. 5. If the user wants to add the other party as a friend again, the other party needs to agree and add the user again.

1. Open the Douyin app, click [Message] at the bottom of the interface, and click the chat conversation entry that needs to be deleted. 2. Long press any chat record, click [Multiple Select], and check the chat records you want to delete. 3. Click the [Delete] button in the lower right corner and select [Confirm deletion] in the pop-up window to permanently delete these records.

Yes, C++ Lambda expressions can support recursion by using std::function: Use std::function to capture a reference to a Lambda expression. With a captured reference, a Lambda expression can call itself recursively.

Practical PHP Tips: Delete the Last Semicolon in the Code When writing PHP code, you often encounter situations where you need to delete the last semicolon in the code. This may be because copy-pasting introduces extra semicolons, or to optimize code style and structure. In this article, we will introduce some methods to remove the last semicolon in PHP code and provide specific code examples. Method 1: Use the substr function The substr function can return a substring of a specified length from a string. we can

The recursive algorithm solves structured problems through function self-calling. The advantage is that it is simple and easy to understand, but the disadvantage is that it is less efficient and may cause stack overflow. The non-recursive algorithm avoids recursion by explicitly managing the stack data structure. The advantage is that it is more efficient and avoids the stack. Overflow, the disadvantage is that the code may be more complex. The choice of recursive or non-recursive depends on the problem and the specific constraints of the implementation.

1. Open the WeChat app, click [Me] in the lower right corner, find and click the [Moments] option. 2. Click [My Moments] in the upper right corner and find the content in the Moments you want to delete on the My Moments interface. 3. Click to enter the details page of this circle of friends, and click the [small trash can] icon to the right of the content release time. 4. Select [OK] in the pop-up window, thus completing the operation of deleting the content in the circle of friends.

Tail recursion optimization (TRO) improves the efficiency of certain recursive calls. It converts tail-recursive calls into jump instructions and saves the context state in registers instead of on the stack, thereby eliminating extra calls and return operations to the stack and improving algorithm efficiency. Using TRO, we can optimize tail recursive functions (such as factorial calculations). By replacing the tail recursive call with a goto statement, the compiler will convert the goto jump into TRO and optimize the execution of the recursive algorithm.
