Union of Linked
Union of Linked
Union of Linked-List :
void insertAtEnd(int d, node*&head, node*&tail)
{
if (head == 0)
head = tail = new node(d);
else
{
tail->next = new node(d);
tail = tail->next;
}
}
void xunion(node*head, node*head2, node*&ansHead, node*&ansTail)
{
while (head != 0)
{
insertAtEnd(head->data, ansHead, ansTail);
head = head->next;
}
while (head2 != 0)
{
bool t = false;
node*temp = ansHead;
while (temp != 0)
{
if (head2->data == temp->data)
{
t = true;
}
temp = temp->next;
}
if (t == false)
insertAtEnd(head2->data, ansHead, ansTail);
head2 = head2->next;
}
}
2.
Intersection of Linked-List :
void intersection(node*head, node*head2, node*&ansHead, node*&ansTail)
{
while (head != 0)
{
node*temp = head2;
bool t = false;
while (temp != 0)
{
if (head->data == temp->data)
{
insertAtEnd(head->data, ansHead, ansTail);
break;
}
temp = temp->next;
}
head = head->next;
}
}
if (b == 0)
return a;
node*ansHead;
if (a->data < b->data)
{
ansHead = a;
a->next = merge(a->next, b);
}
else
{
ansHead = b;
b->next = merge(a, b->next);
}
return ansHead;
}
6.
node*temp = head->next;
head->next = tail->next;
tail->next = head;
head = temp;
reversebyRE(head, tail);
}
void isPalindromeByRE(node*head, node*tail)
{
node*temp = head;
node*mid = midNode(temp);
node*newHead;
if (mid->data == mid->next->data)
{
newHead = mid->next;
}
else
{
newHead = mid->next;
node*n = new node(mid->data);
n->next = newHead;
newHead = n;
}
mid->next = 0;
reversebyRE(newHead, tail);
bool t = 1;
while (temp != 0 || newHead != 0)
{
if (temp->data != newHead->data)
{
t = 0;
break;
}
temp = temp->next;
newHead = newHead->next;
}
if (t == 0)
cout << "No.\n";
else