Remove all common nodes in two linked lists

Here given code implementation process.

// C Program
// Remove all common nodes in two linked lists
#include <stdio.h>
 //For malloc function
#include <stdlib.h>

//Linked List Node
struct Node
{
	int data;
	struct Node *next;
};
//Create a node of linked list
struct Node *create_node(int data)
{
	//Create dynamic node
	struct Node *node = (struct Node *) malloc(sizeof(struct Node));
	if (node == NULL)
	{
		printf("Memory overflow\n");
	}
	else
	{
		//Set initial node value
		node->data = data;
		node->next = NULL;
	}
	return node;
}
//Add new node at end of linked list 
void add_node(struct Node **head, int data)
{
	struct Node *node = create_node(data);
	if ( *head == NULL)
	{
		*head = node;
	}
	else
	{
		struct Node *temp = *head;
		//Find last node
		while (temp->next != NULL)
		{
			temp = temp->next;
		}
		//Add node at last possition
		temp->next = node;
	}
}
//Display linked list element
void display(struct Node *head)
{
	if (head == NULL)
	{
		printf("\nEmpty linked list\n");
		return;
	}
	struct Node *temp = head;
	//iterating linked list elements
	while (temp != NULL)
	{
		if (temp != head)
		{
			printf(" →");
		}
		printf(" %d", temp->data);
		//visit to next node
		temp = temp->next;
	}
	printf(" → NULL\n");
}
//Delete all nodes of a given key in linked list
struct Node *remove_node(struct Node *head, int *changes, int key)
{
	//Define some auxiliary variables
	struct Node *front = head;
	struct Node *tail = NULL;
	struct Node *temp = NULL;
	//iterating linked list elements
	while (front != NULL)
	{
		if (front->data == key)
		{
			// Common node exist
			*changes = 1;
			// When deleted node key exists in linked list
			// Getting a deleted node
			temp = front;
		}
		else
		{
			//Get previous node
			tail = front;
		}
		//Visit to next node
		front = front->next;
		if (temp != NULL)
		{
			if (temp == head)
			{
				//Delete first node of given list
				//visit to next node
				head = head->next;
			}
			else
			{
				// Deleted node are exist in intermediate 
				// or last position of linked list
				// Before delete node, 
				// There left node is connecting to next upcoming node
				tail->next = front;
			}
			//free node
			temp->next = NULL;
			free(temp);
			temp = NULL;
		}
	}
	return head;
}
// Delete common key nodes in two linked lists
void delete_common_nodes(struct Node **head1, struct Node **head2)
{
	//Define some auxiliary variables
	struct Node *front = *head1;
	struct Node *back = NULL;
	int key = 0;
	int changes = 0;
	//iterating first linked list elements
	while (front != NULL)
	{
		key = front->data;
		changes = 0;*head2 = remove_node( *head2, &changes, key);
		if (changes == 1)
		{
			//When common node exist in second linked list
			//Then remove similar node in first linked list
			if ( *head1 == front)
			{
				//When deleting a first node of current first list
				*head1 = remove_node( *head1, &changes, key);
				front = *head1;
			}
			else
			{
				back->next = remove_node(front, &changes, key);
				front = back->next;
			}
		}
		else
		{
			back = front;
			front = front->next;
		}
	}
}
int main()
{
	struct Node *head1 = NULL;
	struct Node *head2 = NULL;
	//Add node in first linked list
	add_node( &head1, 3);
	add_node( &head1, 2);
	add_node( &head1, 4);
	add_node( &head1, 7);
	add_node( &head1, 3);
	add_node( &head1, 6);
	add_node( &head1, 3);
	add_node( &head1, 7);
	//Add node in second linked list
	add_node( &head2, 7);
	add_node( &head2, 3);
	add_node( &head2, 1);
	add_node( &head2, 5);
	add_node( &head2, 7);
	add_node( &head2, 9);
	add_node( &head2, 5);
	//Before effect
	printf(" Before Delete Common Nodes\n");
	// 3 → 2 → 4 → 7 → 3 → 6 → 3 → 7 → NULL
	display(head1);
	//7 → 3 → 1 → 5 → 7 → 9 → 5 → NULL
	display(head2);
	delete_common_nodes( &head1, &head2);
	//After effect
	printf(" After Delete Common Nodes\n");
	//  2 → 4 → 6 → NULL
	display(head1);
	//  1 → 5 → 9 → 5 → NULL
	display(head2);
	return 0;
}

Output

 Before Delete Common Nodes
 3 → 2 → 4 → 7 → 3 → 6 → 3 → 7 → NULL
 7 → 3 → 1 → 5 → 7 → 9 → 5 → NULL
 After Delete Common Nodes
 2 → 4 → 6 → NULL
 1 → 5 → 9 → 5 → NULL
// Java Program
// Remove all common nodes in two linked lists

//Node of LinkedList
class Node
{
	public int data;
	public Node next;
	public Node(int data)
	{
		//Set node value
		this.data = data;
		this.next = null;
	}
}
class MyLinkedList
{
	public Node head;
	public Node tail;
	//Class constructor
	public MyLinkedList()
	{
		this.head = null;
		this.tail = null;
	}
	//insert node at last of linke list
	public void add_node(int data)
	{
		//Create a node
		Node node = new Node(data);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this.tail.next = node;
			this.tail = node;
		}
	}
	//Display linked list element
	public void display()
	{
		if (this.head == null)
		{
			System.out.print("\nEmpty linked list\n");
			return;
		}
		Node temp = this.head;
		System.out.print("Linked List : ");
		//iterating linked list elements
		while (temp != null)
		{
			if (temp != this.head)
			{
				System.out.print(" →");
			}
			System.out.print(" " + temp.data);
			//visit to next node
			temp = temp.next;
		}
		System.out.print(" → NULL\n");
	}
	//Delete all nodes of a given key in linked list
	public boolean remove_node(Node current, Node back, int key)
	{
		//Define some auxiliary variables
		Node front = current;
		Node tail = back;
		Node temp = null;
		boolean changes = false;
		//iterating linked list elements
		while (front != null)
		{
			if (front.data == key)
			{
				// Common node exist
				changes = true;
				// When deleted node key exists in linked list
				// Getting a deleted node
				temp = front;
			}
			else
			{
				//Get previous node
				tail = front;
			}
			//Visit to next node
			front = front.next;
			if (temp != null)
			{
				if (temp == this.head)
				{
					//Delete first node of given list
					//visit to next node
					this.head = this.head.next;
					tail = null;
				}
				else if (tail != null)
				{
					// Deleted node are exist in intermediate 
					// or last position of linked list
					// Before delete node, 
					// There left node is connecting to next upcoming node
					tail.next = front;
				}
				//free node
				temp.next = null;
				temp = null;
			}
		}
		return changes;
	}
	// Delete common key nodes in two linked lists
	public void delete_common_nodes(MyLinkedList other)
	{
		//Define some auxiliary variables
		Node front = this.head;
		Node back = null;
		int key = 0;
		//iterating first linked list elements
		while (front != null)
		{
			key = front.data;
			if (other.remove_node(other.head, null, key))
			{
				//When common node exist in second linked list
				//Then remove similar node in first linked list
				if (this.head == front)
				{
					//When deleting a first node of current first list
					this.remove_node(this.head, null, key);
					front = this.head;
				}
				else
				{
					this.remove_node(front, back, key);
					front = back.next;
				}
			}
			else
			{
				back = front;
				front = front.next;
			}
		}
	}
	public static void main(String[] args)
	{
		MyLinkedList l1 = new MyLinkedList();
		MyLinkedList l2 = new MyLinkedList();
		//Add node in first linked list
		l1.add_node(3);
		l1.add_node(2);
		l1.add_node(4);
		l1.add_node(7);
		l1.add_node(3);
		l1.add_node(6);
		l1.add_node(3);
		l1.add_node(7);
		//Add node in second linked list
		l2.add_node(7);
		l2.add_node(3);
		l2.add_node(1);
		l2.add_node(5);
		l2.add_node(7);
		l2.add_node(9);
		l2.add_node(5);
		//Before effect
		System.out.print(" Before Delete Common Nodes\n");
		// 3 → 2 → 4 → 7 → 3 → 6 → 3 → 7 → NULL
		l1.display();
		//7 → 3 → 1 → 5 → 7 → 9 → 5 → NULL
		l2.display();
		l1.delete_common_nodes(l2);
		//After effect
		System.out.print(" After Delete Common Nodes\n");
		//  2 → 4 → 6 → NULL
		l1.display();
		//  1 → 5 → 9 → 5 → NULL
		l2.display();
	}
}

Output

 Before Delete Common Nodes
Linked List :  3 → 2 → 4 → 7 → 3 → 6 → 3 → 7 → NULL
Linked List :  7 → 3 → 1 → 5 → 7 → 9 → 5 → NULL
 After Delete Common Nodes
Linked List :  2 → 4 → 6 → NULL
Linked List :  1 → 5 → 9 → 5 → NULL
//Include header file
#include <iostream>
using namespace std;

// C++ Program
// Remove all common nodes in two linked lists
//Node of LinkedList

class Node
{
	public: int data;
	Node *next;
	Node(int data)
	{
		//Set node value
		this->data = data;
		this->next = NULL;
	}
};
class MyLinkedList
{
	public: Node *head;
	Node *tail;
	//Class constructor
	MyLinkedList()
	{
		this->head = NULL;
		this->tail = NULL;
	}
	//insert node at last of linke list
	void add_node(int data)
	{
		//Create a node
		Node *node = new Node(data);
		if (this->head == NULL)
		{
			//When linked list empty add first node
			this->head = node;
			this->tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this->tail->next = node;
			this->tail = node;
		}
	}
	//Display linked list element
	void display()
	{
		if (this->head == NULL)
		{
			cout << "\nEmpty linked list\n";
			return;
		}
		Node *temp = this->head;
		cout << "Linked List : ";
		//iterating linked list elements
		while (temp != NULL)
		{
			if (temp != this->head)
			{
				cout << " →";
			}
			cout << " " << temp->data;
			//visit to next node
			temp = temp->next;
		}
		cout << " → NULL\n";
	}
	//Delete all nodes of a given key in linked list
	bool remove_node(Node *current, Node *back, int key)
	{
		//Define some auxiliary variables
		Node *front = current;
		Node *tail = back;
		Node *temp = NULL;
		bool changes = false;
		//iterating linked list elements
		while (front != NULL)
		{
			if (front->data == key)
			{
				// Common node exist
				changes = true;
				// When deleted node key exists in linked list
				// Getting a deleted node
				temp = front;
			}
			else
			{
				//Get previous node
				tail = front;
			}
			//Visit to next node
			front = front->next;
			if (temp != NULL)
			{
				if (temp == this->head)
				{
					//Delete first node of given list
					//visit to next node
					this->head = this->head->next;
					tail = NULL;
				}
				else if (tail != NULL)
				{
					// Deleted node are exist in intermediate 
					// or last position of linked list
					// Before delete node, 
					// There left node is connecting to next upcoming node
					tail->next = front;
				}
				//free node
				temp->next = NULL;
				temp = NULL;
			}
		}
		return changes;
	}
	// Delete common key nodes in two linked lists
	void delete_common_nodes(MyLinkedList *other)
	{
		//Define some auxiliary variables
		Node *front = this->head;
		Node *back = NULL;
		int key = 0;
		//iterating first linked list elements
		while (front != NULL)
		{
			key = front->data;
			if (other->remove_node(other->head, NULL, key) == true)
			{
				//When common node exist in second linked list
				//Then remove similar node in first linked list
				if (this->head == front)
				{
					//When deleting a first node of current first list
					this->remove_node(this->head, NULL, key);
					front = this->head;
				}
				else
				{
					this->remove_node(front, back, key);
					front = back->next;
				}
			}
			else
			{
				back = front;
				front = front->next;
			}
		}
	}
};
int main()
{
	MyLinkedList l1 = MyLinkedList();
	MyLinkedList l2 = MyLinkedList();
	//Add node in first linked list
	l1.add_node(3);
	l1.add_node(2);
	l1.add_node(4);
	l1.add_node(7);
	l1.add_node(3);
	l1.add_node(6);
	l1.add_node(3);
	l1.add_node(7);
	//Add node in second linked list
	l2.add_node(7);
	l2.add_node(3);
	l2.add_node(1);
	l2.add_node(5);
	l2.add_node(7);
	l2.add_node(9);
	l2.add_node(5);
	//Before effect
	cout << " Before Delete Common Nodes\n";
	// 3 → 2 → 4 → 7 → 3 → 6 → 3 → 7 → NULL
	l1.display();
	//7 → 3 → 1 → 5 → 7 → 9 → 5 → NULL
	l2.display();
	l1.delete_common_nodes( &l2);
	//After effect
	cout << " After Delete Common Nodes\n";
	//  2 → 4 → 6 → NULL
	l1.display();
	//  1 → 5 → 9 → 5 → NULL
	l2.display();
	return 0;
}

Output

 Before Delete Common Nodes
Linked List :  3 → 2 → 4 → 7 → 3 → 6 → 3 → 7 → NULL
Linked List :  7 → 3 → 1 → 5 → 7 → 9 → 5 → NULL
 After Delete Common Nodes
Linked List :  2 → 4 → 6 → NULL
Linked List :  1 → 5 → 9 → 5 → NULL
//Include namespace system
using System;

// C# Program
// Remove all common nodes in two linked lists

//Node of LinkedList
class Node
{
	public int data;
	public Node next;
	public Node(int data)
	{
		//Set node value
		this.data = data;
		this.next = null;
	}
}
class MyLinkedList
{
	public Node head;
	public Node tail;
	//Class constructor
	public MyLinkedList()
	{
		this.head = null;
		this.tail = null;
	}
	//insert node at last of linke list
	public void add_node(int data)
	{
		//Create a node
		Node node = new Node(data);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this.tail.next = node;
			this.tail = node;
		}
	}
	//Display linked list element
	public void display()
	{
		if (this.head == null)
		{
			Console.Write("\nEmpty linked list\n");
			return;
		}
		Node temp = this.head;
		Console.Write("Linked List : ");
		//iterating linked list elements
		while (temp != null)
		{
			if (temp != this.head)
			{
				Console.Write(" →");
			}
			Console.Write(" " + temp.data);
			//visit to next node
			temp = temp.next;
		}
		Console.Write(" → NULL\n");
	}
	//Delete all nodes of a given key in linked list
	public Boolean remove_node(Node current, Node back, int key)
	{
		//Define some auxiliary variables
		Node front = current;
		Node tail = back;
		Node temp = null;
		Boolean changes = false;
		//iterating linked list elements
		while (front != null)
		{
			if (front.data == key)
			{
				// Common node exist
				changes = true;
				// When deleted node key exists in linked list
				// Getting a deleted node
				temp = front;
			}
			else
			{
				//Get previous node
				tail = front;
			}
			//Visit to next node
			front = front.next;
			if (temp != null)
			{
				if (temp == this.head)
				{
					//Delete first node of given list
					//visit to next node
					this.head = this.head.next;
					tail = null;
				}
				else if (tail != null)
				{
					// Deleted node are exist in intermediate 
					// or last position of linked list
					// Before delete node, 
					// There left node is connecting to next upcoming node
					tail.next = front;
				}
				//free node
				temp.next = null;
				temp = null;
			}
		}
		return changes;
	}
	// Delete common key nodes in two linked lists
	public void delete_common_nodes(MyLinkedList other)
	{
		//Define some auxiliary variables
		Node front = this.head;
		Node back = null;
		int key = 0;
		//iterating first linked list elements
		while (front != null)
		{
			key = front.data;
			if (other.remove_node(other.head, null, key))
			{
				//When common node exist in second linked list
				//Then remove similar node in first linked list
				if (this.head == front)
				{
					//When deleting a first node of current first list
					this.remove_node(this.head, null, key);
					front = this.head;
				}
				else
				{
					this.remove_node(front, back, key);
					front = back.next;
				}
			}
			else
			{
				back = front;
				front = front.next;
			}
		}
	}
	public static void Main(String[] args)
	{
		MyLinkedList l1 = new MyLinkedList();
		MyLinkedList l2 = new MyLinkedList();
		//Add node in first linked list
		l1.add_node(3);
		l1.add_node(2);
		l1.add_node(4);
		l1.add_node(7);
		l1.add_node(3);
		l1.add_node(6);
		l1.add_node(3);
		l1.add_node(7);
		//Add node in second linked list
		l2.add_node(7);
		l2.add_node(3);
		l2.add_node(1);
		l2.add_node(5);
		l2.add_node(7);
		l2.add_node(9);
		l2.add_node(5);
		//Before effect
		Console.Write(" Before Delete Common Nodes\n");
		// 3 → 2 → 4 → 7 → 3 → 6 → 3 → 7 → NULL
		l1.display();
		//7 → 3 → 1 → 5 → 7 → 9 → 5 → NULL
		l2.display();
		l1.delete_common_nodes(l2);
		//After effect
		Console.Write(" After Delete Common Nodes\n");
		//  2 → 4 → 6 → NULL
		l1.display();
		//  1 → 5 → 9 → 5 → NULL
		l2.display();
	}
}

Output

 Before Delete Common Nodes
Linked List :  3 → 2 → 4 → 7 → 3 → 6 → 3 → 7 → NULL
Linked List :  7 → 3 → 1 → 5 → 7 → 9 → 5 → NULL
 After Delete Common Nodes
Linked List :  2 → 4 → 6 → NULL
Linked List :  1 → 5 → 9 → 5 → NULL
<?php
// Php Program
// Remove all common nodes in two linked lists

//Node of LinkedList
class Node
{
	public $data;
	public $next;

	function __construct($data)
	{
		//Set node value
		$this->data = $data;
		$this->next = null;
	}
}
class MyLinkedList
{
	public $head;
	public $tail;
	//Class constructor
	function __construct()
	{
		$this->head = null;
		$this->tail = null;
	}
	//insert node at last of linke list
	public	function add_node($data)
	{
		//Create a node
		$node = new Node($data);
		if ($this->head == null)
		{
			//When linked list empty add first node
			$this->head = $node;
			$this->tail = $node;
		}
		else
		{
			//Add new node at end of linked list
			$this->tail->next = $node;
			$this->tail = $node;
		}
	}
	//Display linked list element
	public	function display()
	{
		if ($this->head == null)
		{
			echo "\nEmpty linked list\n";
			return;
		}
		$temp = $this->head;
		echo "Linked List : ";
		//iterating linked list elements
		while ($temp != null)
		{
			if ($temp != $this->head)
			{
				echo " →";
			}
			echo " ". $temp->data;
			//visit to next node
			$temp = $temp->next;
		}
		echo " → NULL\n";
	}
	//Delete all nodes of a given key in linked list
	public	function remove_node($current, $back, $key)
	{
		//Define some auxiliary variables
		$front = $current;
		$tail = $back;
		$temp = null;
		$changes = false;
		//iterating linked list elements
		while ($front != null)
		{
			if ($front->data == $key)
			{
				// Common node exist
				$changes = true;
				// When deleted node key exists in linked list
				// Getting a deleted node
				$temp = $front;
			}
			else
			{
				//Get previous node
				$tail = $front;
			}
			//Visit to next node
			$front = $front->next;
			if ($temp != null)
			{
				if ($temp == $this->head)
				{
					//Delete first node of given list
					//visit to next node
					$this->head = $this->head->next;
					$tail = null;
				}
				else if ($tail != null)
				{
					// Deleted node are exist in intermediate 
					// or last position of linked list
					// Before delete node, 
					// There left node is connecting to next upcoming node
					$tail->next = $front;
				}
				//free node
				$temp->next = null;
				$temp = null;
			}
		}
		return $changes;
	}
	// Delete common key nodes in two linked lists
	public	function delete_common_nodes($other)
	{
		//Define some auxiliary variables
		$front = $this->head;
		$back = null;
		$key = 0;
		//iterating first linked list elements
		while ($front != null)
		{
			$key = $front->data;
			if ($other->remove_node($other->head, null, $key))
			{
				//When common node exist in second linked list
				//Then remove similar node in first linked list
				if ($this->head == $front)
				{
					//When deleting a first node of current first list
					$this->remove_node($this->head, null, $key);
					$front = $this->head;
				}
				else
				{
					$this->remove_node($front, $back, $key);
					$front = $back->next;
				}
			}
			else
			{
				$back = $front;
				$front = $front->next;
			}
		}
	}
}

function main()
{
	$l1 = new MyLinkedList();
	$l2 = new MyLinkedList();
	//Add node in first linked list
	$l1->add_node(3);
	$l1->add_node(2);
	$l1->add_node(4);
	$l1->add_node(7);
	$l1->add_node(3);
	$l1->add_node(6);
	$l1->add_node(3);
	$l1->add_node(7);
	//Add node in second linked list
	$l2->add_node(7);
	$l2->add_node(3);
	$l2->add_node(1);
	$l2->add_node(5);
	$l2->add_node(7);
	$l2->add_node(9);
	$l2->add_node(5);
	//Before effect
	echo " Before Delete Common Nodes\n";
	// 3 → 2 → 4 → 7 → 3 → 6 → 3 → 7 → NULL
	$l1->display();
	//7 → 3 → 1 → 5 → 7 → 9 → 5 → NULL
	$l2->display();
	$l1->delete_common_nodes($l2);
	//After effect
	echo " After Delete Common Nodes\n";
	//  2 → 4 → 6 → NULL
	$l1->display();
	//  1 → 5 → 9 → 5 → NULL
	$l2->display();
}
main();

Output

 Before Delete Common Nodes
Linked List :  3 → 2 → 4 → 7 → 3 → 6 → 3 → 7 → NULL
Linked List :  7 → 3 → 1 → 5 → 7 → 9 → 5 → NULL
 After Delete Common Nodes
Linked List :  2 → 4 → 6 → NULL
Linked List :  1 → 5 → 9 → 5 → NULL
// Node Js Program
// Remove all common nodes in two linked lists

//Node of LinkedList
class Node
{
	constructor(data)
	{
		//Set node value
		this.data = data;
		this.next = null;
	}
}
class MyLinkedList
{
	//Class constructor
	constructor()
	{
		this.head = null;
		this.tail = null;
	}
	//insert node at last of linke list
	add_node(data)
	{
		//Create a node
		var node = new Node(data);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this.tail.next = node;
			this.tail = node;
		}
	}
	//Display linked list element
	display()
	{
		if (this.head == null)
		{
			process.stdout.write("\nEmpty linked list\n");
			return;
		}
		var temp = this.head;
		process.stdout.write("Linked List : ");
		//iterating linked list elements
		while (temp != null)
		{
			if (temp != this.head)
			{
				process.stdout.write(" →");
			}
			process.stdout.write(" " + temp.data);
			//visit to next node
			temp = temp.next;
		}
		process.stdout.write(" → NULL\n");
	}
	//Delete all nodes of a given key in linked list
	remove_node(current, back, key)
	{
		//Define some auxiliary variables
		var front = current;
		var tail = back;
		var temp = null;
		var changes = false;
		//iterating linked list elements
		while (front != null)
		{
			if (front.data == key)
			{
				// Common node exist
				changes = true;
				// When deleted node key exists in linked list
				// Getting a deleted node
				temp = front;
			}
			else
			{
				//Get previous node
				tail = front;
			}
			//Visit to next node
			front = front.next;
			if (temp != null)
			{
				if (temp == this.head)
				{
					//Delete first node of given list
					//visit to next node
					this.head = this.head.next;
					tail = null;
				}
				else if (tail != null)
				{
					// Deleted node are exist in intermediate 
					// or last position of linked list
					// Before delete node, 
					// There left node is connecting to next upcoming node
					tail.next = front;
				}
				//free node
				temp.next = null;
				temp = null;
			}
		}
		return changes;
	}
	// Delete common key nodes in two linked lists
	delete_common_nodes(other)
	{
		//Define some auxiliary variables
		var front = this.head;
		var back = null;
		var key = 0;
		//iterating first linked list elements
		while (front != null)
		{
			key = front.data;
			if (other.remove_node(other.head, null, key))
			{
				//When common node exist in second linked list
				//Then remove similar node in first linked list
				if (this.head == front)
				{
					//When deleting a first node of current first list
					this.remove_node(this.head, null, key);
					front = this.head;
				}
				else
				{
					this.remove_node(front, back, key);
					front = back.next;
				}
			}
			else
			{
				back = front;
				front = front.next;
			}
		}
	}
}

function main()
{
	var l1 = new MyLinkedList();
	var l2 = new MyLinkedList();
	//Add node in first linked list
	l1.add_node(3);
	l1.add_node(2);
	l1.add_node(4);
	l1.add_node(7);
	l1.add_node(3);
	l1.add_node(6);
	l1.add_node(3);
	l1.add_node(7);
	//Add node in second linked list
	l2.add_node(7);
	l2.add_node(3);
	l2.add_node(1);
	l2.add_node(5);
	l2.add_node(7);
	l2.add_node(9);
	l2.add_node(5);
	//Before effect
	process.stdout.write(" Before Delete Common Nodes\n");
	// 3 → 2 → 4 → 7 → 3 → 6 → 3 → 7 → NULL
	l1.display();
	//7 → 3 → 1 → 5 → 7 → 9 → 5 → NULL
	l2.display();
	l1.delete_common_nodes(l2);
	//After effect
	process.stdout.write(" After Delete Common Nodes\n");
	//  2 → 4 → 6 → NULL
	l1.display();
	//  1 → 5 → 9 → 5 → NULL
	l2.display();
}
main();

Output

 Before Delete Common Nodes
Linked List :  3 → 2 → 4 → 7 → 3 → 6 → 3 → 7 → NULL
Linked List :  7 → 3 → 1 → 5 → 7 → 9 → 5 → NULL
 After Delete Common Nodes
Linked List :  2 → 4 → 6 → NULL
Linked List :  1 → 5 → 9 → 5 → NULL
#  Python 3 Program
#  Remove all common nodes in two linked lists

# Node of LinkedList
class Node :
	
	def __init__(self, data) :
		# Set node value
		self.data = data
		self.next = None
	

class MyLinkedList :
	
	# Class constructor
	def __init__(self) :
		self.head = None
		self.tail = None
	
	# insert node at last of linke list
	def add_node(self, data) :
		# Create a node
		node = Node(data)
		if (self.head == None) :
			# When linked list empty add first node
			self.head = node
			self.tail = node
		else :
			# Add new node at end of linked list
			self.tail.next = node
			self.tail = node
		
	
	# Display linked list element
	def display(self) :
		if (self.head == None) :
			print("\nEmpty linked list\n", end = "")
			return
		
		temp = self.head
		print("Linked List : ", end = "")
		# iterating linked list elements
		while (temp != None) :
			if (temp != self.head) :
				print(" →", end = "")
			
			print(" ", temp.data, end = "")
			# visit to next node
			temp = temp.next
		
		print(" → NULL\n", end = "")
	
	# Delete all nodes of a given key in linked list
	def remove_node(self, current, back, key) :
		# Define some auxiliary variables
		front = current
		tail = back
		temp = None
		changes = False
		# iterating linked list elements
		while (front != None) :
			if (front.data == key) :
				#  Common node exist
				changes = True
				#  When deleted node key exists in linked list
				#  Getting a deleted node
				temp = front
			else :
				# Get previous node
				tail = front
			
			# Visit to next node
			front = front.next
			if (temp != None) :
				if (temp == self.head) :
					# Delete first node of given list
					# visit to next node
					self.head = self.head.next
					tail = None
				
				elif(tail != None) :
					#  Deleted node are exist in intermediate 
					#  or last position of linked list
					#  Before delete node, 
					#  There left node is connecting to next upcoming node
					tail.next = front
				
				# free node
				temp.next = None
				temp = None
			
		
		return changes
	
	#  Delete common key nodes in two linked lists
	def delete_common_nodes(self, other) :
		# Define some auxiliary variables
		front = self.head
		back = None
		key = 0
		# iterating first linked list elements
		while (front != None) :
			key = front.data
			if (other.remove_node(other.head, None, key)) :
				# When common node exist in second linked list
				# Then remove similar node in first linked list
				if (self.head == front) :
					# When deleting a first node of current first list
					self.remove_node(self.head, None, key)
					front = self.head
				else :
					self.remove_node(front, back, key)
					front = back.next
				
			else :
				back = front
				front = front.next
			
		
	

def main() :
	l1 = MyLinkedList()
	l2 = MyLinkedList()
	# Add node in first linked list
	l1.add_node(3)
	l1.add_node(2)
	l1.add_node(4)
	l1.add_node(7)
	l1.add_node(3)
	l1.add_node(6)
	l1.add_node(3)
	l1.add_node(7)
	# Add node in second linked list
	l2.add_node(7)
	l2.add_node(3)
	l2.add_node(1)
	l2.add_node(5)
	l2.add_node(7)
	l2.add_node(9)
	l2.add_node(5)
	# Before effect
	print(" Before Delete Common Nodes\n", end = "")
	#  3 → 2 → 4 → 7 → 3 → 6 → 3 → 7 → NULL
	l1.display()
	# 7 → 3 → 1 → 5 → 7 → 9 → 5 → NULL
	l2.display()
	l1.delete_common_nodes(l2)
	# After effect
	print(" After Delete Common Nodes\n", end = "")
	#   2 → 4 → 6 → NULL
	l1.display()
	#   1 → 5 → 9 → 5 → NULL
	l2.display()

if __name__ == "__main__": main()

Output

 Before Delete Common Nodes
Linked List :   3 →  2 →  4 →  7 →  3 →  6 →  3 →  7 → NULL
Linked List :   7 →  3 →  1 →  5 →  7 →  9 →  5 → NULL
 After Delete Common Nodes
Linked List :   2 →  4 →  6 → NULL
Linked List :   1 →  5 →  9 →  5 → NULL
#  Ruby Program
#  Remove all common nodes in two linked lists

# Node of LinkedList
class Node 

	# Define the accessor and reader of class Node  
	attr_reader :data, :next
	attr_accessor :data, :next

	def initialize(data)
	
		# Set node value
		self.data = data
		self.next = nil
	end
end
class MyLinkedList 

	# Define the accessor and reader of class MyLinkedList  
	attr_reader :head, :tail
	attr_accessor :head, :tail


	
	# Class constructor
	def initialize()
	
		self.head = nil
		self.tail = nil
	end
	# insert node at last of linke list
	def add_node(data)
	
		# Create a node
		node = Node.new(data)
		if (self.head == nil)
		
			# When linked list empty add first node
			self.head = node
			self.tail = node
		else
		
			# Add new node at end of linked list
			self.tail.next = node
			self.tail = node
		end
	end
	# Display linked list element
	def display()
	
		if (self.head == nil)
		
			print("\nEmpty linked list\n")
			return
		end
		temp = self.head
		print("Linked List : ")
		# iterating linked list elements
		while (temp != nil)
		
			if (temp != self.head)
			
				print(" →")
			end
			print(" ", temp.data)
			# visit to next node
			temp = temp.next
		end
		print(" → NULL\n")
	end
	# Delete all nodes of a given key in linked list
	def remove_node(current, back, key)
	
		# Define some auxiliary variables
		front = current
		tail = back
		temp = nil
		changes = false
		# iterating linked list elements
		while (front != nil)
		
			if (front.data == key)
			
				#  Common node exist
				changes = true
				#  When deleted node key exists in linked list
				#  Getting a deleted node
				temp = front
			else
			
				# Get previous node
				tail = front
			end
			# Visit to next node
			front = front.next
			if (temp != nil)
			
				if (temp == self.head)
				
					# Delete first node of given list
					# visit to next node
					self.head = self.head.next
					tail = nil
				elsif(tail != nil)
				
					#  Deleted node are exist in intermediate 
					#  or last position of linked list
					#  Before delete node, 
					#  There left node is connecting to next upcoming node
					tail.next = front
				end
				# free node
				temp.next = nil
				temp = nil
			end
		end
		return changes
	end
	#  Delete common key nodes in two linked lists
	def delete_common_nodes(other)
	
		# Define some auxiliary variables
		front = self.head
		back = nil
		key = 0
		# iterating first linked list elements
		while (front != nil)
		
			key = front.data
			if (other.remove_node(other.head, nil, key))
			
				# When common node exist in second linked list
				# Then remove similar node in first linked list
				if (self.head == front)
				
					# When deleting a first node of current first list
					self.remove_node(self.head, nil, key)
					front = self.head
				else
				
					self.remove_node(front, back, key)
					front = back.next
				end
			else
			
				back = front
				front = front.next
			end
		end
	end
end
def main()

	l1 = MyLinkedList.new()
	l2 = MyLinkedList.new()
	# Add node in first linked list
	l1.add_node(3)
	l1.add_node(2)
	l1.add_node(4)
	l1.add_node(7)
	l1.add_node(3)
	l1.add_node(6)
	l1.add_node(3)
	l1.add_node(7)
	# Add node in second linked list
	l2.add_node(7)
	l2.add_node(3)
	l2.add_node(1)
	l2.add_node(5)
	l2.add_node(7)
	l2.add_node(9)
	l2.add_node(5)
	# Before effect
	print(" Before Delete Common Nodes\n")
	#  3 → 2 → 4 → 7 → 3 → 6 → 3 → 7 → NULL
	l1.display()
	# 7 → 3 → 1 → 5 → 7 → 9 → 5 → NULL
	l2.display()
	l1.delete_common_nodes(l2)
	# After effect
	print(" After Delete Common Nodes\n")
	#   2 → 4 → 6 → NULL
	l1.display()
	#   1 → 5 → 9 → 5 → NULL
	l2.display()
end
main()

Output

 Before Delete Common Nodes
Linked List :  3 → 2 → 4 → 7 → 3 → 6 → 3 → 7 → NULL
Linked List :  7 → 3 → 1 → 5 → 7 → 9 → 5 → NULL
 After Delete Common Nodes
Linked List :  2 → 4 → 6 → NULL
Linked List :  1 → 5 → 9 → 5 → NULL
// Scala Program
// Remove all common nodes in two linked lists

//Node of LinkedList
class Node(var data: Int,
	var next: Node)
{
	def this(data: Int)
	{
		this(data, null);
	}
}
class MyLinkedList(var head: Node,
	var tail: Node)
{
	//Class constructor
	def this()
	{
		this(null, null);
	}
	//insert node at last of linke list
	def add_node(data: Int): Unit = {
		//Create a node
		var node: Node = new Node(data);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this.tail.next = node;
			this.tail = node;
		}
	}
	//Display linked list element
	def display(): Unit = {
		if (this.head == null)
		{
			print("\nEmpty linked list\n");
			return;
		}
		var temp: Node = this.head;
		print("Linked List : ");
		//iterating linked list elements
		while (temp != null)
		{
			if (temp != this.head)
			{
				print(" →");
			}
			print(" " + temp.data);
			//visit to next node
			temp = temp.next;
		}
		print(" → NULL\n");
	}
	//Delete all nodes of a given key in linked list
	def remove_node(current: Node, back: Node, key: Int): Boolean = {
		//Define some auxiliary variables
		var front: Node = current;
		var tail: Node = back;
		var temp: Node = null;
		var changes: Boolean = false;
		//iterating linked list elements
		while (front != null)
		{
			if (front.data == key)
			{
				// Common node exist
				changes = true;
				// When deleted node key exists in linked list
				// Getting a deleted node
				temp = front;
			}
			else
			{
				//Get previous node
				tail = front;
			}
			//Visit to next node
			front = front.next;
			if (temp != null)
			{
				if (temp == this.head)
				{
					//Delete first node of given list
					//visit to next node
					this.head = this.head.next;
					tail = null;
				}
				else if (tail != null)
				{
					// Deleted node are exist in intermediate 
					// or last position of linked list
					// Before delete node, 
					// There left node is connecting to next upcoming node
					tail.next = front;
				}
				//free node
				temp.next = null;
				temp = null;
			}
		}
		return changes;
	}
	// Delete common key nodes in two linked lists
	def delete_common_nodes(other: MyLinkedList): Unit = {
		//Define some auxiliary variables
		var front: Node = this.head;
		var back: Node = null;
		var key: Int = 0;
		//iterating first linked list elements
		while (front != null)
		{
			key = front.data;
			if (other.remove_node(other.head, null, key))
			{
				//When common node exist in second linked list
				//Then remove similar node in first linked list
				if (this.head == front)
				{
					//When deleting a first node of current first list
					this.remove_node(this.head, null, key);
					front = this.head;
				}
				else
				{
					this.remove_node(front, back, key);
					front = back.next;
				}
			}
			else
			{
				back = front;
				front = front.next;
			}
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var l1: MyLinkedList = new MyLinkedList();
		var l2: MyLinkedList = new MyLinkedList();
		//Add node in first linked list
		l1.add_node(3);
		l1.add_node(2);
		l1.add_node(4);
		l1.add_node(7);
		l1.add_node(3);
		l1.add_node(6);
		l1.add_node(3);
		l1.add_node(7);
		//Add node in second linked list
		l2.add_node(7);
		l2.add_node(3);
		l2.add_node(1);
		l2.add_node(5);
		l2.add_node(7);
		l2.add_node(9);
		l2.add_node(5);
		//Before effect
		print(" Before Delete Common Nodes\n");
		// 3 → 2 → 4 → 7 → 3 → 6 → 3 → 7 → NULL
		l1.display();
		//7 → 3 → 1 → 5 → 7 → 9 → 5 → NULL
		l2.display();
		l1.delete_common_nodes(l2);
		//After effect
		print(" After Delete Common Nodes\n");
		//  2 → 4 → 6 → NULL
		l1.display();
		//  1 → 5 → 9 → 5 → NULL
		l2.display();
	}
}

Output

 Before Delete Common Nodes
Linked List :  3 → 2 → 4 → 7 → 3 → 6 → 3 → 7 → NULL
Linked List :  7 → 3 → 1 → 5 → 7 → 9 → 5 → NULL
 After Delete Common Nodes
Linked List :  2 → 4 → 6 → NULL
Linked List :  1 → 5 → 9 → 5 → NULL
// Swift 4 Program
// Remove all common nodes in two linked lists

//Node of LinkedList
class Node
{
	var data: Int;
	var next: Node? ;
	init(_ data: Int)
	{
		//Set node value
		self.data = data;
		self.next = nil;
	}
}
class MyLinkedList
{
	var head: Node? ;
	var tail: Node? ;
	//Class constructor
	init()
	{
		self.head = nil;
		self.tail = nil;
	}
	//insert node at last of linke list
	func add_node(_ data: Int)
	{
		//Create a node
		let node: Node? = Node(data);
		if (self.head == nil)
		{
			//When linked list empty add first node
			self.head = node;
			self.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			self.tail!.next = node;
			self.tail = node;
		}
	}
	//Display linked list element
	func display()
	{
		if (self.head == nil)
		{
			print("\nEmpty linked list\n", terminator: "");
			return;
		}
		var temp: Node? = self.head;
		print("Linked List : ", terminator: "");
		//iterating linked list elements
		while (temp != nil)
		{
			if (!(temp === self.head))
			{
				print(" →", terminator: "");
			}
			print(" ", temp!.data, terminator: "");
			//visit to next node
			temp = temp!.next;
		}
		print(" → NULL\n", terminator: "");
	}
	//Delete all nodes of a given key in linked list
	func remove_node(_ current: Node? , _ back : Node? , _ key : Int) -> Bool
	{
		//Define some auxiliary variables
		var front: Node? = current;
		var tail: Node? = back;
		var temp: Node? = nil;
		var changes: Bool = false;
		//iterating linked list elements
		while (front != nil)
		{
			if (front!.data == key)
			{
				// Common node exist
				changes = true;
				// When deleted node key exists in linked list
				// Getting a deleted node
				temp = front;
			}
			else
			{
				//Get previous node
				tail = front;
			}
			//Visit to next node
			front = front!.next;
			if (temp != nil)
			{
				if (temp === self.head)
				{
					//Delete first node of given list
					//visit to next node
					self.head = self.head!.next;
					tail = nil;
				}
				else if (tail != nil)
				{
					// Deleted node are exist in intermediate 
					// or last position of linked list
					// Before delete node, 
					// There left node is connecting to next upcoming node
					tail!.next = front;
				}
				//free node
				temp!.next = nil;
				temp = nil;
			}
		}
		return changes;
	}
	// Delete common key nodes in two linked lists
	func delete_common_nodes(_ other: MyLinkedList)
	{
		//Define some auxiliary variables
		var front: Node? = self.head;
		var back: Node? = nil;
		var key: Int = 0;
		//iterating first linked list elements
		while (front != nil)
		{
			key = front!.data;
			if (other.remove_node(other.head, nil, key))
			{
				//When common node exist in second linked list
				//Then remove similar node in first linked list
				if (self.head === front)
				{
					//When deleting a first node of current first list
					let _ = self.remove_node(self.head, nil, key);
					front = self.head;
				}
				else
				{
					let _ =  self.remove_node(front, back, key);
					front = back!.next;
				}
			}
			else
			{
				back = front;
				front = front!.next;
			}
		}
	}
}
func main()
{
	let l1: MyLinkedList = MyLinkedList();
	let l2: MyLinkedList = MyLinkedList();
	//Add node in first linked list
	l1.add_node(3);
	l1.add_node(2);
	l1.add_node(4);
	l1.add_node(7);
	l1.add_node(3);
	l1.add_node(6);
	l1.add_node(3);
	l1.add_node(7);
	//Add node in second linked list
	l2.add_node(7);
	l2.add_node(3);
	l2.add_node(1);
	l2.add_node(5);
	l2.add_node(7);
	l2.add_node(9);
	l2.add_node(5);
	//Before effect
	print(" Before Delete Common Nodes\n", terminator: "");
	// 3 → 2 → 4 → 7 → 3 → 6 → 3 → 7 → NULL
	l1.display();
	//7 → 3 → 1 → 5 → 7 → 9 → 5 → NULL
	l2.display();
	l1.delete_common_nodes(l2);
	//After effect
	print(" After Delete Common Nodes\n", terminator: "");
	//  2 → 4 → 6 → NULL
	l1.display();
	//  1 → 5 → 9 → 5 → NULL
	l2.display();
}
main();

Output

 Before Delete Common Nodes
Linked List :   3 →  2 →  4 →  7 →  3 →  6 →  3 →  7 → NULL
Linked List :   7 →  3 →  1 →  5 →  7 →  9 →  5 → NULL
 After Delete Common Nodes
Linked List :   2 →  4 →  6 → NULL
Linked List :   1 →  5 →  9 →  5 → NULL


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved