#include <bits/stdc++.h>
using
namespace
std;
struct
Node
{
char
data;
struct
Node* next;
};
void
push(
struct
Node **head_ref,
int
new_data)
{
struct
Node* new_node =
new
Node;
new_node->data = new_data;
new_node->next = *head_ref;
*head_ref = new_node;
}
struct
Node *reverseList(
struct
Node *head_ref)
{
struct
Node *current, *prev, *next;
current = head_ref;
prev = NULL;
while
(current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return
prev;
}
void
printLastNNode(
struct
Node* head,
int
n)
{
if
(n <= 0)
return
;
struct
Node* ref_ptr = head;
while
(ref_ptr != NULL && n--)
ref_ptr = ref_ptr->next;
Node *main_ptr = head;
while
(ref_ptr != NULL) {
main_ptr = main_ptr->next;
ref_ptr = ref_ptr->next;
}
while
(main_ptr != NULL)
{
cout << main_ptr->data;
main_ptr = main_ptr->next;
}
}
void
longestCommSuffix(Node *h1, Node *h2)
{
h1 = reverseList(h1);
h2 = reverseList(h2);
Node *temp1 = h1, *temp2 = h2;
int
count = 0;
while
(temp1!=NULL&&temp2!=NULL)
{
if
(temp1 -> data != temp2 -> data)
break
;
count++;
temp1 = temp1 -> next;
temp2 = temp2 -> next;
}
h1 = reverseList(h1);
h2 = reverseList(h2);
printLastNNode(h1, count);
}
int
main()
{
struct
Node *h1 = NULL, *h2 = NULL;
push(&h1,
'g'
);
push(&h1,
'n'
);
push(&h1,
'i'
);
push(&h1,
'k'
);
push(&h1,
'l'
);
push(&h1,
'a'
);
push(&h1,
'w'
);
push(&h2,
'g'
);
push(&h2,
'n'
);
push(&h2,
'i'
);
push(&h2,
'n'
);
push(&h2,
'e'
);
push(&h2,
't'
);
push(&h2,
's'
);
push(&h2,
'i'
);
push(&h2,
'l'
);
longestCommSuffix(h1, h2);
return
0;
}