Longest common suffix of two linked lists

Given two singly linked lists, find the Longest common suffix of two linked lists. If there are no common characters which are suffixes, return the minimum length of the two linked lists.


Input : list1 = w -> a -> l -> k -> i -> n -> g
        list2 = l -> i -> s -> t -> e -> n -> i -> n -> g
Output :i -> n -> g

Input : list1 = p -> a -> r -> t -> y
        list2 = p -> a -> r -> t -> y -> i -> n -> g
Output :p -> a -> r -> t -> y

A simple solution is to use auxiliary arrays to store linked lists. Then print longest commons suffix of two arrays.

The above solution requires extra space. We can save space by first doing reverse of both linked lists. After reversing, we can easily find length of longest common prefix. Reversing again to get the original lists back.
One important point here is, order of elements. We need to print nodes from n-th to end. We use the above found count and print nodes in required order using two pointer approach.





// C++ program to find the Longest Common
// suffix in linked lists
#include <bits/stdc++.h>
using namespace std;
/* Linked list node */
struct Node
    char data;
    struct Node* next;
/* Function to insert a node at the beginning of
   the linked list */
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;
/* Function to reverse the linked list */
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;
// Utility function to print last n nodes
void printLastNNode(struct Node* head, int n)
    // if n == 0
    if (n <= 0)
    // Move reference pointer n positions ahead
    struct Node* ref_ptr = head;
    while (ref_ptr != NULL && n--)
        ref_ptr = ref_ptr->next;
    // Now move main and reference pointers at
    // same speed. By the end of this loop,
    // reference pointer would point to end and
    // main pointer would point to n-th node
    // from end.
    Node *main_ptr = head;
    while (ref_ptr != NULL) {
        main_ptr = main_ptr->next;
        ref_ptr = ref_ptr->next;
    // Print last n nodes.
    while (main_ptr != NULL)
       cout << main_ptr->data;
       main_ptr = main_ptr->next;
// Prints the Longest Common suffix in 
// linked lists
void longestCommSuffix(Node *h1, Node *h2)
    // Reverse Both Linked list
    h1 = reverseList(h1);
    h2 = reverseList(h2);
    // Now we print common nodes from head
    Node *temp1 = h1, *temp2 = h2;
    int count = 0;
    while (temp1!=NULL&&temp2!=NULL)
        // If a node is not common, break
        if (temp1 -> data != temp2 -> data)
        // Keep printing while there are
        // common nodes.
        temp1 = temp1 -> next;
        temp2 = temp2 -> next;
    // Reversing linked lists to retain
    // original lists.
    h1 = reverseList(h1);
    h2 = reverseList(h2);
    printLastNNode(h1, count);
// Driver program to test above
int main()
    struct Node *h1 = NULL, *h2 = NULL;
    // creating the 1 linked list
    // creating the 2 linked list
    longestCommSuffix(h1, h2);
    return 0;




Time Complexity : O(N)

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.