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)
- 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
- 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
- Add Two Numbers Represented by Linked Lists | Set 3
- Add two numbers represented by linked lists | Set 2
- Merge K sorted linked lists | Set 1
- Check if two Linked Lists are permutations of each other
- Merge Sort for Linked Lists
- Intersection of two Sorted Linked Lists
- Add two numbers represented by linked lists | Set 1
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.