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.
Time Complexity : O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- First common element in two linked lists
- Find count of common nodes in two Doubly Linked Lists
- Remove the common nodes in two Singly Linked Lists
- Find common elements in three linked lists
- Create a linked list from two linked lists by choosing max element at each position
- Longest Common Prefix using Linked List
- Find the common nodes in two singly linked list
- Write a function to get the intersection point of two Linked Lists
- Add two numbers represented by linked lists | Set 1
- Union and Intersection of two Linked Lists
- Add two numbers represented by linked lists | Set 2
- Compare two strings represented as linked lists
- Merge two sorted linked lists such that merged list is in reverse order
- In-place Merge two linked lists without changing links of first list
- Subtract Two Numbers represented as Linked Lists
- Multiply two numbers represented as linked lists into a third list
- Union and Intersection of two linked lists | Set-2 (Using Merge Sort)
- Multiply two numbers represented by Linked Lists
- Union and Intersection of two linked lists | Set-3 (Hashing)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.