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 common elements in three linked lists
- Remove the common nodes in two Singly Linked Lists
- Find count of common nodes in two Doubly Linked Lists
- Longest Common Prefix using Linked List
- Create a linked list from two linked lists by choosing max element at each position
- Program for longest common directory path
- Identical Linked Lists
- Merge K sorted linked lists | Set 1
- Add Two Numbers Represented by Linked Lists | Set 3
- Union and Intersection of two Linked Lists
- Add two numbers represented by linked lists | Set 1
- Merge Sort for Linked Lists
- Intersection of two Sorted Linked Lists
- Merge two sorted linked lists
- Check if two Linked Lists are permutations of each other
- Add two numbers represented by linked lists | Set 2
- Find the common nodes in two singly linked list
- Difference of two Linked Lists using Merge sort
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.