# Merge two sorted linked list without duplicates

Merge two sorted linked list of size n1 and n2. The duplicates in two linked list should be present only once in the final sorted linked list.

Examples:

`Input : list1: 1->1->4->5->7        list2: 2->4->7->9Output : 1 2 4 5 7 9`

Approach: Following are the steps:

1. Merge the two sorted linked list in sorted manner. Refer recursive approach of this post. Let the final obtained list be head.

Implementation:

## C++

 `// C++ implementation to merge two sorted linked list``// without duplicates``#include ` `using` `namespace` `std;` `// structure of a node``struct` `Node {``    ``int` `data;``    ``Node* next;``};` `// function to get a new node``Node* getNode(``int` `data)``{``    ``// allocate space``    ``Node* temp = (Node*)``malloc``(``sizeof``(Node));` `    ``// put in data``    ``temp->data = data;``    ``temp->next = NULL;``    ``return` `temp;``}` `// function to merge two sorted linked list``// in a sorted manner``Node* sortedMerge(``struct` `Node* a, ``struct` `Node* b)``{``    ``Node* result = NULL;` `    ``/* Base cases */``    ``if` `(a == NULL)``        ``return` `(b);``    ``else` `if` `(b == NULL)``        ``return` `(a);` `    ``/* Pick either a or b, and recur */``    ``if` `(a->data <= b->data) {``        ``result = a;``        ``result->next = sortedMerge(a->next, b);``    ``}``    ``else` `{``        ``result = b;``        ``result->next = sortedMerge(a, b->next);``    ``}``    ``return` `(result);``}` `/* The function removes duplicates from a sorted list */``void` `removeDuplicates(Node* head)``{``    ``/* Pointer to traverse the linked list */``    ``Node* current = head;` `    ``/* Pointer to store the next pointer of a node to be deleted*/``    ``Node* next_next;` `    ``/* do nothing if the list is empty */``    ``if` `(current == NULL)``        ``return``;` `    ``/* Traverse the list till last node */``    ``while` `(current->next != NULL) {` `        ``/* Compare current node with next node */``        ``if` `(current->data == current->next->data) {` `            ``/* The sequence of steps is important*/``            ``next_next = current->next->next;``            ``free``(current->next);``            ``current->next = next_next;``        ``}``        ``else` `/* This is tricky: only advance if no deletion */``        ``{``            ``current = current->next;``        ``}``    ``}``}` `// function to merge two sorted linked list``// without duplicates``Node* sortedMergeWithoutDuplicates(Node* head1, Node* head2)``{``    ``// merge two linked list in sorted manner``    ``Node* head = sortedMerge(head1, head2);` `    ``// remove duplicates from the list 'head'``    ``removeDuplicates(head);` `    ``return` `head;``}` `// function to print the linked list``void` `printList(Node* head)``{``    ``while` `(head != NULL) {``        ``cout << head->data << ``" "``;``        ``head = head->next;``    ``}``}` `// Driver program to test above``int` `main()``{``    ``// head1: 1->1->4->5->7``    ``Node* head1 = getNode(1);``    ``head1->next = getNode(1);``    ``head1->next->next = getNode(4);``    ``head1->next->next->next = getNode(5);``    ``head1->next->next->next->next = getNode(7);` `    ``// head2: 2->4->7->9``    ``Node* head2 = getNode(2);``    ``head2->next = getNode(4);``    ``head2->next->next = getNode(7);``    ``head2->next->next->next = getNode(9);` `    ``Node* head3;` `    ``head3 = sortedMergeWithoutDuplicates(head1, head2);` `    ``printList(head3);` `    ``return` `0;``}`

## Java

 `// Java implementation to merge two sorted linked list``// without duplicates``import` `java.io.*;` `class` `GFG {` `    ``// structure of a node``    ``class` `Node {``        ``int` `data;``        ``Node next;``    ``}` `    ``// function to get a new node``    ``public` `Node getNode(``int` `data)``    ``{``        ``Node temp = ``new` `Node();``        ``temp.data = data;``        ``temp.next = ``null``;``        ``return` `temp;``    ``}` `    ``// function to merge two sorted linked list in a sorted``    ``// manner``    ``static` `Node sortedMerge(Node a, Node b)``    ``{``        ``Node result = ``null``;``        ``/* Base cases */``        ``if` `(a == ``null``) {``            ``return` `b;``        ``}``        ``else` `if` `(b == ``null``) {``            ``return` `a;``        ``}``        ``/* Pick either a or b, and recur */``        ``if` `(a.data <= b.data) {``            ``result = a;``            ``result.next = sortedMerge(a.next, b);``        ``}``        ``else` `{``            ``result = b;``            ``result.next = sortedMerge(a, b.next);``        ``}``        ``return` `result;``    ``}` `    ``/* The function removes duplicates from a sorted list */``    ``static` `void` `removeDuplicates(Node head)``    ``{``        ``/* Pointer to traverse the linked list */``        ``Node current = head;``      ` `        ``/* Pointer to store the next pointer of a node to be``         ``* deleted*/``        ``Node next_next;` `        ``/* do nothing if the list is empty */``        ``if` `(current == ``null``) {``            ``return``;``        ``}` `        ``/* Traverse the list till last node */``        ``while` `(current.next != ``null``) ``        ``{``          ` `            ``/* Compare current node with next node */``            ``if` `(current.data == current.next.data)``            ``{``              ` `                ``/* The sequence of steps is important*/``                ``next_next = current.next.next;``                ``current.next = next_next;``            ``}``            ``else` `{ ``/* This is tricky: only advance if no``                      ``deletion */``                ``current = current.next;``            ``}``        ``}``    ``}` `    ``// function to merge two sorted linked list without``    ``// duplicates``    ``public` `Node sortedMergeWithoutDuplicates(Node head1,``                                             ``Node head2)``    ``{` `        ``// merge two linked list in sorted manner``        ``Node head = sortedMerge(head1, head2);` `        ``// remove duplicates from the list 'head'``        ``removeDuplicates(head);` `        ``return` `head;``    ``}` `    ``// function to print the linked list``    ``public` `void` `printList(Node head)``    ``{``        ``while` `(head != ``null``) {``            ``System.out.print(head.data + ``" "``);``            ``head = head.next;``        ``}``    ``}` `    ``public` `static` `void` `main(String[] args)``    ``{` `        ``GFG l = ``new` `GFG();` `        ``// head1 : 1->1->4->5->7``        ``Node head1 = l.getNode(``1``);``        ``head1.next = l.getNode(``1``);``        ``head1.next.next = l.getNode(``4``);``        ``head1.next.next.next = l.getNode(``5``);``        ``head1.next.next.next.next = l.getNode(``7``);` `        ``// head2 : 2->4->7->9``        ``Node head2 = l.getNode(``2``);``        ``head2.next = l.getNode(``4``);``        ``head2.next.next = l.getNode(``7``);``        ``head2.next.next.next = l.getNode(``9``);` `        ``Node head3;` `        ``head3``            ``= l.sortedMergeWithoutDuplicates(head1, head2);` `        ``l.printList(head3);``    ``}``}` `// This code is contributed by lokeshmvs21.`

## Python3

 `# Python3 implementation to merge two ``# sorted linked list without duplicates`` ` `# Structure of a node``class` `Node:``    ` `    ``def` `__init__(``self``, data):``        ` `        ``self``.data ``=` `data``        ``self``.``next` `=` `None`` ` `# Function to get a new node``def` `getNode(data):``    ` `    ``# Allocate space``    ``temp ``=` `Node(data)``    ``return` `temp` `# Function to merge two sorted linked ``# list in a sorted manner``def` `sortedMerge(a, b):``    ` `    ``result ``=` `None`` ` `    ``# Base cases ``    ``if` `(a ``=``=` `None``):``        ``return``(b)``    ``elif` `(b ``=``=` `None``):``        ``return``(a)`` ` `    ``# Pick either a or b, and recur ``    ``if` `(a.data <``=` `b.data):``        ``result ``=` `a``        ``result.``next` `=` `sortedMerge(a.``next``, b)``    ``else``:``        ``result ``=` `b``        ``result.``next` `=` `sortedMerge(a, b.``next``)` `    ``return``(result)``    ` `# The function removes duplicates``# from a sorted list ``def` `removeDuplicates(head):` `    ``# Pointer to traverse the linked list ``    ``current ``=` `head`` ` `    ``# Pointer to store the next pointer``    ``# of a node to be deleted``    ``next_next ``=` `None`` ` `    ``# Do nothing if the list is empty ``    ``if` `(current ``=``=` `None``):``        ``return`` ` `    ``# Traverse the list till last node ``    ``while` `(current.``next` `!``=` `None``):`` ` `        ``# Compare current node with next node ``        ``if` `(current.data ``=``=` `current.``next``.data):`` ` `            ``# The sequence of steps is important``            ``next_next ``=` `current.``next``.``next``            ``del` `(current.``next``)``            ``current.``next` `=` `next_next``        ``else``: ``            ` `            ``# This is tricky: only advance ``            ``# if no deletion ``            ``current ``=` `current.``next``    ` `# Function to merge two sorted linked list``# without duplicates``def` `sortedMergeWithoutDuplicates(head1, head2):` `    ``# Merge two linked list in sorted manner``    ``head ``=` `sortedMerge(head1, head2)`` ` `    ``# Remove duplicates from the list 'head'``    ``removeDuplicates(head)`` ` `    ``return` `head` `# Function to print the linked list``def` `printList(head):` `    ``while` `(head !``=` `None``):``        ``print``(head.data, end ``=` `' '``)    ``        ``head ``=` `head.``next``    ` `# Driver code``if` `__name__``=``=``'__main__'``:``    ` `    ``# head1: 1.1.4.5.7``    ``head1 ``=` `getNode(``1``)``    ``head1.``next` `=` `getNode(``1``)``    ``head1.``next``.``next` `=` `getNode(``4``)``    ``head1.``next``.``next``.``next` `=` `getNode(``5``)``    ``head1.``next``.``next``.``next``.``next` `=` `getNode(``7``)`` ` `    ``# head2: 2.4.7.9``    ``head2 ``=` `getNode(``2``)``    ``head2.``next` `=` `getNode(``4``)``    ``head2.``next``.``next` `=` `getNode(``7``)``    ``head2.``next``.``next``.``next` `=` `getNode(``9``)`` ` `    ``head3 ``=` `sortedMergeWithoutDuplicates(``        ``head1, head2)`` ` `    ``printList(head3)`` ` `# This code is contributed by rutvik_56`

## C#

 `// C# implementation to merge two sorted linked list``// without duplicates``using` `System;``public` `class` `GFG{` `  ``// structure of a node``  ``class` `Node {``    ``public` `int` `data;``    ``public` `Node next;``  ``}` `  ``// function to get a new node``  ``Node getNode(``int` `data)``  ``{``    ``Node temp = ``new` `Node();``    ``temp.data = data;``    ``temp.next = ``null``;``    ``return` `temp;``  ``}` `  ``// function to merge two sorted linked list in a sorted``  ``// manner``  ``static` `Node sortedMerge(Node a, Node b)``  ``{``    ``Node result = ``null``;``    ``/* Base cases */``    ``if` `(a == ``null``) {``      ``return` `b;``    ``}``    ``else` `if` `(b == ``null``) {``      ``return` `a;``    ``}``    ``/* Pick either a or b, and recur */``    ``if` `(a.data <= b.data) {``      ``result = a;``      ``result.next = sortedMerge(a.next, b);``    ``}``    ``else` `{``      ``result = b;``      ``result.next = sortedMerge(a, b.next);``    ``}``    ``return` `result;``  ``}` `  ``/* The function removes duplicates from a sorted list */``  ``static` `void` `removeDuplicates(Node head)``  ``{``    ``/* Pointer to traverse the linked list */``    ``Node current = head;` `    ``/* Pointer to store the next pointer of a node to be``         ``* deleted*/``    ``Node next_next;` `    ``/* do nothing if the list is empty */``    ``if` `(current == ``null``) {``      ``return``;``    ``}` `    ``/* Traverse the list till last node */``    ``while` `(current.next != ``null``)``    ``{` `      ``/* Compare current node with next node */``      ``if` `(current.data == current.next.data)``      ``{` `        ``/* The sequence of steps is important*/``        ``next_next = current.next.next;``        ``current.next = next_next;``      ``}``      ``else` `{ ``/* This is tricky: only advance if no``                      ``deletion */``        ``current = current.next;``      ``}``    ``}``  ``}` `  ``// function to merge two sorted linked list without``  ``// duplicates``  ``Node sortedMergeWithoutDuplicates(Node head1,``                                    ``Node head2)``  ``{` `    ``// merge two linked list in sorted manner``    ``Node head = sortedMerge(head1, head2);` `    ``// remove duplicates from the list 'head'``    ``removeDuplicates(head);` `    ``return` `head;``  ``}` `  ``// function to print the linked list``  ``void` `printList(Node head)``  ``{``    ``while` `(head != ``null``) {``      ``Console.Write(head.data + ``" "``);``      ``head = head.next;``    ``}``  ``}` `  ``static` `public` `void` `Main (){` `    ``GFG l = ``new` `GFG();` `    ``// head1 : 1->1->4->5->7``    ``Node head1 = l.getNode(1);``    ``head1.next = l.getNode(1);``    ``head1.next.next = l.getNode(4);``    ``head1.next.next.next = l.getNode(5);``    ``head1.next.next.next.next = l.getNode(7);` `    ``// head2 : 2->4->7->9``    ``Node head2 = l.getNode(2);``    ``head2.next = l.getNode(4);``    ``head2.next.next = l.getNode(7);``    ``head2.next.next.next = l.getNode(9);` `    ``Node head3;` `    ``head3``      ``= l.sortedMergeWithoutDuplicates(head1, head2);` `    ``l.printList(head3);``  ``}``}` `// This code is contributed by lokeshmvs21.`

## Javascript

 `// JavaScript implementation to merge two sorted linked list``// without duplicates` `// structure to get a new node``class Node{``    ``constructor(data){``        ``this``.data = data;``        ``this``.next = ``null``;``    ``}``}` `// function to get a new node``function` `getNode(data){``    ``// allocate space and put data``    ``let temp = ``new` `Node(data);``    ``return` `temp;``}` `// function to merge two sorted linked list``// in a sorted manner``function` `sortedMerge(a, b){``    ``let result = ``null``;``    ` `    ``// base case``    ``if``(a == ``null``)``        ``return` `b;``    ``else` `if``(b == ``null``)``        ``return` `a;``        ` `    ``// Pick either a or b, and recur``    ``if` `(a.data <= b.data) {``        ``result = a;``        ``result.next = sortedMerge(a.next, b);``    ``}``    ``else` `{``        ``result = b;``        ``result.next = sortedMerge(a, b.next);``    ``}``    ``return` `result;``}` `// The function removes duplicates from a sorted list``function` `removeDuplicates(head){``    ``// Pointer to traverse the linked list ``    ``let current = head;`` ` `    ``// Pointer to store the next pointer of a node to be deleted``    ``let next_next;`` ` `    ``// do nothing if the list is empty ``    ``if` `(current == ``null``)``        ``return``;`` ` `    ``// Traverse the list till last node ``    ``while` `(current.next != ``null``) {`` ` `        ``// Compare current node with next node ``        ``if` `(current.data == current.next.data) {`` ` `            ``// The sequence of steps is important``            ``next_next = current.next.next;``            ``current.next = next_next;``        ``}``        ``else` `// This is tricky: only advance if no deletion ``        ``{``            ``current = current.next;``        ``}``    ``}``}` `// function to merge two sorted linked list``// without duplicates``function` `sortedMergeWithoutDuplicates(head1, head2){``    ``// merge two linked list in sorted manner``    ``let head = sortedMerge(head1, head2);`` ` `    ``// remove duplicates from the list 'head'``    ``removeDuplicates(head);`` ` `    ``return` `head;``}` `// function to print the linked list``function` `printList(head)``{``    ``while` `(head != ``null``) {``        ``console.log(head.data + ``" "``);``        ``head = head.next;``    ``}``}` `// Driver program to test above` `// head1: 1->1->4->5->7``let head1 = getNode(1);``head1.next = getNode(1);``head1.next.next = getNode(4);``head1.next.next.next = getNode(5);``head1.next.next.next.next = getNode(7);` `// head2: 2->4->7->9``let head2 = getNode(2);``head2.next = getNode(4);``head2.next.next = getNode(7);``head2.next.next.next = getNode(9);` `let head3 = sortedMergeWithoutDuplicates(head1, head2);``printList(head3);` `// This code is contributed by Yash Agarwal(yashagawral2852002)`

Output
```1 2 4 5 7 9

```

Complexity Analysis:

• Time complexity: O(n1 + n2).
• Auxiliary Space: O(1).

Approach: This approach uses an iterative approach to merge two sorted linked lists without duplicates.

Steps:

• It creates a dummy node and a tail pointer to efficiently merge the two lists in sorted order. The dummy node helps in handling the merged list easily.
• As the lists are merged, it simultaneously removes duplicates in a single pass through the merged list.

Below is the implementation of the above approach:

## C++

 `//C++ code for the above approach``#include ``using` `namespace` `std;` `struct` `Node {``    ``int` `data;``    ``Node* next;``};` `Node* getNode(``int` `data)``{``    ``Node* temp = ``new` `Node;``    ``temp->data = data;``    ``temp->next = NULL;``    ``return` `temp;``}` `Node* sortedMergeWithoutDuplicates(Node* head1, Node* head2)``{``    ``// Create a dummy node to simplify merging``    ``Node* dummy = ``new` `Node;``    ``Node* tail = dummy;` `    ``// Merge the two sorted linked lists``    ``while` `(head1 != NULL && head2 != NULL) {``        ``if` `(head1->data < head2->data) {``            ``// Append the smaller node to the merged list``            ``tail->next = head1;``            ``head1 = head1->next;``        ``}``        ``else` `if` `(head1->data > head2->data) {``            ``// Append the smaller node to the merged list``            ``tail->next = head2;``            ``head2 = head2->next;``        ``}``        ``else` `{``            ``// If both nodes have the same value, append only one of them to avoid duplicates``            ``tail->next = head1;``            ``head1 = head1->next;``            ``head2 = head2->next;``        ``}``        ``tail = tail->next;``        ``tail->next = NULL; ``// Mark the end of the merged list``    ``}` `    ``// If any elements left in either of the lists, add them to the merged list``    ``if` `(head1 != NULL) {``        ``tail->next = head1;``    ``}``    ``if` `(head2 != NULL) {``        ``tail->next = head2;``    ``}` `    ``// Remove duplicates from the merged list``    ``Node* current = dummy->next;``    ``while` `(current != NULL && current->next != NULL) {``        ``if` `(current->data == current->next->data) {``            ``// Remove the duplicate node from the merged list``            ``Node* temp = current->next;``            ``current->next = current->next->next;``            ``delete` `temp; ``// Free the memory of the duplicate node``        ``}``        ``else` `{``            ``// Move to the next node``            ``current = current->next;``        ``}``    ``}` `    ``Node* result = dummy->next;``    ``delete` `dummy; ``// Free the memory of the dummy node``    ``return` `result;``}` `void` `printList(Node* head)``{``    ``// Traverse the linked list and print its elements``    ``while` `(head != NULL) {``        ``cout << head->data << ``" "``;``        ``head = head->next;``    ``}``    ``cout << endl;``}` `int` `main()``{``    ``// Create two sorted linked lists``    ``Node* head1 = getNode(1);``    ``head1->next = getNode(1);``    ``head1->next->next = getNode(4);``    ``head1->next->next->next = getNode(5);``    ``head1->next->next->next->next = getNode(7);` `    ``Node* head2 = getNode(2);``    ``head2->next = getNode(4);``    ``head2->next->next = getNode(7);``    ``head2->next->next->next = getNode(9);` `    ``// Merge the two lists without duplicates and print the result``    ``Node* head3 = sortedMergeWithoutDuplicates(head1, head2);``    ``printList(head3);` `    ``return` `0;``}`

## Java

 `public` `class` `Main {` `    ``// Node class to represent a node in the linked list``    ``static` `class` `Node {``        ``int` `data;``        ``Node next;` `        ``Node(``int` `data) {``            ``this``.data = data;``            ``this``.next = ``null``;``        ``}``    ``}` `    ``// Function to merge two sorted linked lists without duplicates``    ``static` `Node sortedMergeWithoutDuplicates(Node head1, Node head2) {``        ``// Create a dummy node to simplify merging``        ``Node dummy = ``new` `Node(-``1``);``        ``Node tail = dummy;` `        ``// Merge the two sorted linked lists``        ``while` `(head1 != ``null` `&& head2 != ``null``) {``            ``if` `(head1.data < head2.data) {``                ``// Append the smaller node to the merged list``                ``tail.next = head1;``                ``head1 = head1.next;``            ``} ``else` `if` `(head1.data > head2.data) {``                ``// Append the smaller node to the merged list``                ``tail.next = head2;``                ``head2 = head2.next;``            ``} ``else` `{``                ``// If both nodes have the same value, ``                ``// append only one of them to avoid duplicates``                ``tail.next = head1;``                ``head1 = head1.next;``                ``head2 = head2.next;``            ``}``            ``tail = tail.next;``            ``tail.next = ``null``; ``// Mark the end of the merged list``        ``}` `        ``// If any elements left in either of the lists, add them to the merged list``        ``if` `(head1 != ``null``) {``            ``tail.next = head1;``        ``}``        ``if` `(head2 != ``null``) {``            ``tail.next = head2;``        ``}` `        ``// Remove duplicates from the merged list``        ``Node current = dummy.next;``        ``while` `(current != ``null` `&& current.next != ``null``) {``            ``if` `(current.data == current.next.data) {``                ``// Remove the duplicate node from the merged list``                ``Node temp = current.next;``                ``current.next = current.next.next;``                ``temp = ``null``; ``// Free the memory of the duplicate node``            ``} ``else` `{``                ``// Move to the next node``                ``current = current.next;``            ``}``        ``}` `        ``return` `dummy.next;``    ``}` `    ``// Function to print a linked list``    ``static` `void` `printList(Node head) {``        ``// Traverse the linked list and print its elements``        ``while` `(head != ``null``) {``            ``System.out.print(head.data + ``" "``);``            ``head = head.next;``        ``}``        ``System.out.println();``    ``}` `    ``public` `static` `void` `main(String[] args) {``        ``// Create two sorted linked lists``        ``Node head1 = ``new` `Node(``1``);``        ``head1.next = ``new` `Node(``1``);``        ``head1.next.next = ``new` `Node(``4``);``        ``head1.next.next.next = ``new` `Node(``5``);``        ``head1.next.next.next.next = ``new` `Node(``7``);` `        ``Node head2 = ``new` `Node(``2``);``        ``head2.next = ``new` `Node(``4``);``        ``head2.next.next = ``new` `Node(``7``);``        ``head2.next.next.next = ``new` `Node(``9``);` `        ``// Merge the two lists without duplicates and print the result``        ``Node head3 = sortedMergeWithoutDuplicates(head1, head2);``        ``printList(head3);``    ``}``}``// This code is contributed by rambabuguphka`

## Python3

 `class` `Node:``    ``def` `__init__(``self``, data):``        ``self``.data ``=` `data``        ``self``.``next` `=` `None` `def` `sortedMergeWithoutDuplicates(head1, head2):``    ``dummy ``=` `Node(``0``)``    ``tail ``=` `dummy` `    ``while` `head1 ``is` `not` `None` `and` `head2 ``is` `not` `None``:``        ``if` `head1.data < head2.data:``            ``tail.``next` `=` `head1``            ``head1 ``=` `head1.``next``        ``elif` `head1.data > head2.data:``            ``tail.``next` `=` `head2``            ``head2 ``=` `head2.``next``        ``else``:``            ``# Skip duplicates``            ``head1 ``=` `head1.``next``            ``head2 ``=` `head2.``next``        ``tail ``=` `tail.``next` `    ``if` `head1 ``is` `not` `None``:``        ``tail.``next` `=` `head1``    ``if` `head2 ``is` `not` `None``:``        ``tail.``next` `=` `head2` `    ``current ``=` `dummy.``next``    ``while` `current ``is` `not` `None` `and` `current.``next` `is` `not` `None``:``        ``if` `current.data ``=``=` `current.``next``.data:``            ``temp ``=` `current.``next``            ``current.``next` `=` `current.``next``.``next``            ``del` `temp``        ``else``:``            ``current ``=` `current.``next` `    ``result ``=` `dummy.``next``    ``return` `result` `def` `printList(head):``    ``while` `head ``is` `not` `None``:``        ``print``(head.data, end``=``" "``)``        ``head ``=` `head.``next``    ``print``()` `if` `__name__ ``=``=` `"__main__"``:``    ``# Create two sorted linked lists``    ``head1 ``=` `Node(``1``)``    ``head1.``next` `=` `Node(``1``)``    ``head1.``next``.``next` `=` `Node(``4``)``    ``head1.``next``.``next``.``next` `=` `Node(``5``)``    ``head1.``next``.``next``.``next``.``next` `=` `Node(``7``)` `    ``head2 ``=` `Node(``2``)``    ``head2.``next` `=` `Node(``4``)``    ``head2.``next``.``next` `=` `Node(``7``)``    ``head2.``next``.``next``.``next` `=` `Node(``9``)` `    ``# Merge the two lists without duplicates and print the result``    ``head3 ``=` `sortedMergeWithoutDuplicates(head1, head2)``    ``printList(head3)`

## C#

 `// C# code for the above approach``using` `System;` `public` `class` `Node {``    ``public` `int` `data;``    ``public` `Node next;``}` `public` `class` `GFG {``    ``// Function to create a new node``    ``static` `Node GetNode(``int` `data)``    ``{``        ``Node temp = ``new` `Node();``        ``temp.data = data;``        ``temp.next = ``null``;``        ``return` `temp;``    ``}` `    ``// Function to merge two sorted linked lists without``    ``// duplicates``    ``static` `Node SortedMergeWithoutDuplicates(Node head1,``                                             ``Node head2)``    ``{``        ``// Create a dummy node to simplify merging``        ``Node dummy = ``new` `Node();``        ``Node tail = dummy;` `        ``// Merge the two sorted linked lists``        ``while` `(head1 != ``null` `&& head2 != ``null``) {``            ``if` `(head1.data < head2.data) {``                ``// Append the smaller node to the merged``                ``// list``                ``tail.next = head1;``                ``head1 = head1.next;``            ``}``            ``else` `if` `(head1.data > head2.data) {``                ``// Append the smaller node to the merged``                ``// list``                ``tail.next = head2;``                ``head2 = head2.next;``            ``}``            ``else` `{``                ``// If both nodes have the same value, append``                ``// only one of them to avoid duplicates``                ``tail.next = head1;``                ``head1 = head1.next;``                ``head2 = head2.next;``            ``}``            ``tail = tail.next;``            ``tail.next``                ``= ``null``; ``// Mark the end of the merged list``        ``}` `        ``// If any elements left in either of the lists, add``        ``// them to the merged list``        ``if` `(head1 != ``null``) {``            ``tail.next = head1;``        ``}``        ``if` `(head2 != ``null``) {``            ``tail.next = head2;``        ``}` `        ``// Remove duplicates from the merged list``        ``Node current = dummy.next;``        ``while` `(current != ``null` `&& current.next != ``null``) {``            ``if` `(current.data == current.next.data) {``                ``// Remove the duplicate node from the merged``                ``// list``                ``_ = current.next;``                ``current.next = current.next.next;``                ``// Free the memory of the duplicate node``                ``_ = ``null``;``            ``}``            ``else` `{``                ``// Move to the next node``                ``current = current.next;``            ``}``        ``}` `        ``Node result = dummy.next;``        ``// Free the memory of the dummy node``        ``dummy = ``null``;``        ``return` `result;``    ``}` `    ``// Function to print the linked list``    ``static` `void` `PrintList(Node head)``    ``{``        ``// Traverse the linked list and print its elements``        ``while` `(head != ``null``) {``            ``Console.Write(head.data + ``" "``);``            ``head = head.next;``        ``}``        ``Console.WriteLine();``    ``}` `    ``// Main method``    ``public` `static` `void` `Main(``string``[] args)``    ``{``        ``// Create two sorted linked lists``        ``Node head1 = GetNode(1);``        ``head1.next = GetNode(1);``        ``head1.next.next = GetNode(4);``        ``head1.next.next.next = GetNode(5);``        ``head1.next.next.next.next = GetNode(7);` `        ``Node head2 = GetNode(2);``        ``head2.next = GetNode(4);``        ``head2.next.next = GetNode(7);``        ``head2.next.next.next = GetNode(9);` `        ``// Merge the two lists without duplicates and print``        ``// the result``        ``Node head3``            ``= SortedMergeWithoutDuplicates(head1, head2);``        ``PrintList(head3);``    ``}``}` `// This code is contributed by Susobhan Akhuli`

## Javascript

 ``

Output
```1 2 4 5 7 9

```

Time Complexity: O(M + N), Where M and N are the size of the list1 and list2 respectively.
Auxiliary Space: O(M+N), Function call stack space

Exercise: Get the final sorted linked list without duplicates in a single traversal of the two lists.

Previous
Next